How to create a shorcode with an extensive PHP code?

Asked

Viewed 57 times

1

How I create a shortcode with this code here? When I upload and place the category in the post it loads the groups and attributes. It is a spreadsheet with Profits and Losses based on category, attributes and groups. The goal is to create a shortcode to be uploaded to a survey form, where the form creates a new post and sends it to a database with a value more or less in this pattern!

a:2:{i:57;s:8:"situação";i:61;s:8:"condição";}

Remembering to generate this code was chosen

// make text input field

which is in the complete code below:

<div id="real-tb-attributes" class="real-tab-content">

<?php $cats = get_product_cats($post->ID);

    if ($cats) {
        $cat = $cats[62];
        $cat_id = $cat->term_id;
    }  ?>

                <?php if ($cat) {
                    // get cat groups
                    $groups = get_real_cat_groups($cat_id);
                    $groups_data = get_real_groups_data();
                    $attrs_data = get_real_attributes_data();

                    if (real_is_array($groups)) {
                        $count = 0; ?>
                        <ul class="real-data-pils">
                            <?php foreach ($groups as $group) { ?>
                                <li data-pil="#real-pil-<?php echo $group; ?>"<?php if ($count == 0) echo ' class="active"'; ?>><?php echo $groups_data[$group]['name']; ?></li>
                                <?php $count++;
                            } ?>
                        </ul>

                        <div class="real-pil-container">
                            <?php $count = 0;
                            foreach ($groups as $group) {
                                $group_data = $groups_data[$group];
                                $group_values = get_real_product_attributes($post->ID, $group); ?>
                                <div id="real-pil-<?php echo $group; ?>" class="real-pil-content<?php if ($count == 0) echo ' active'; ?>">
                                    <?php if ($group_data['attrs']) { ?>
                                        <div class="real-group-inputs">
                                            <ul class="real-input-filelds">
                                                <?php if (real_is_array($group_data['attrs'])) {
                                                    foreach ($group_data['attrs'] as $attr_id) {
                                                        $attr = $attrs_data[$attr_id];
                                                        $attr_val = (isset($group_values[$attr_id])) ? $group_values[$attr_id] : ''; ?>
                                                        <li>
                                                            <div class="real-col-1">
                                                                <label for="attr-input-<?php echo $attr_id; ?>"><?php echo $attr['name']; ?></label>
                                                            </div>
                                                            <div class="real-col-5">
                                                                <?php // switch the input types
                                                                switch ($attr['meta']['type']) {
                                                                    case 'text' :
                                                                        // make text input field
                                                                        echo '<input type="text" id="attr-input-' .$attr_id .'" name="real-attr[' .$group .'][' .$attr_id .']" class="real-text-input" value="' .$attr_val .'" />';
                                                                    break;

                                                                    case 'check' :
                                                                        // make checkbox input field
                                                                        echo '<input type="hidden" name="real-attr[' .$group .'][' .$attr_id .']" value="No" />';
                                                                        echo '<input type="checkbox" id="attr-input-' .$attr_id .'" name="real-attr[' .$group .'][' .$attr_id .']" class="real-checkbox" value="Yes"' .(($attr_val == 'Yes') ? ' checked="checked"' : '') .' />';
                                                                    break;

                                                                    case 'date' :
                                                                        // make date input field
                                                                        echo '<input type="text" id="attr-input-' .$attr_id .'" name="real-attr[' .$group .'][' .$attr_id .']" class="real-date-input real-text-input" value="' .$attr_val .'" />';
                                                                    break;

                                                                    case 'textarea' :
                                                                        // make textarea input field
                                                                        echo '<textarea id="attr-input-' .$attr_id .'" name="real-attr[' .$group .'][' .$attr_id .']" class="real-textarea" rows="4">' .$attr_val .'</textarea>';
                                                                    break;

                                                                    case 'select' :
                                                                        // make select box
                                                                        echo '<div class="real-select-label"><select id="attr-input-' .$attr_id .'" name="real-attr[' .$group .'][' .$attr_id .']" class="real-select-box">';
                                                                        foreach ($attr['meta']['options'] as $option) {
                                                                            echo '<option value="' .$option .'"' .(($option == $attr_val) ? ' selected="selected"' : '') .'>' .$option .'</option>';
                                                                        }
                                                                        echo '</select></div>';
                                                                    break;

                                                                    case 'mselect' :
                                                                        // make multi select box
                                                                        $attr_val = (array) $attr_val;
                                                                        echo '<select id="attr-input-' .$attr_id .'" name="real-attr[' .$group .'][' .$attr_id .']" size="' .count($attr['meta']['options']) .'" class="real-select-box" multiple>';
                                                                        foreach ($attr['meta']['options'] as $option) {
                                                                            echo '<option value="' .$option .'"' .((in_array($option, $attr_val)) ? ' selected="selected"' : '') .'>' .$option .'</option>';
                                                                        }
                                                                        echo '</select>';
                                                                    break;
                                                                } ?>
                                                            </div>
                                                        </li>
                                                    <?php }
                                                } ?>
                                            </ul>
                                        </div>
                                    <?php } ?>
                                </div>
                                <?php $count++;
                            } ?>
                        </div>
                    <?php } else { ?>
                        <p><?php _e('No Groups found for this category, you may add groups by editing category.', 'real-text'); ?></p>
                    <?php }
                } else { ?>
                    <p><?php _e('Please select a Category to continue.', 'real-text'); ?></p>
                <?php } ?>
            </div>

1 answer

1


Brute force solution, use ob_start() and see if it works. See:

function my_shortcode() {
    ob_start();
    ?> <HTML> <?php /* PHP */ ?><etc> ... <?php
    $output_string = ob_get_contents();
    ob_end_clean();
    return $output_string;
}

Another possibility when you have such extensive PHP code is to use the syntax heredoc. See:

Basically, separate PHP from HTML. A Wordpress shortcode never prints anything, it has to return a valid HTML string. With heredoc it is possible to create a complex HTML populated by simple PHP variables built beforehand.

# considerando que no post tem [meu_shortcode id="25"]
add_shortcode( 'meu_shortcode', function($atts,$content){
    $complexo = fazerLoop($atts['id']);
    $final = <<<HTML
    <div>O ID é <strong>{$atts['id']}</strong></div>
    <div>$complexo</div>
HTML;
    return $final;
});

function fazerLoop($id){
    # fazer loop complexo e retornar HTML
    return '<ul><li></li></ul>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.