How to create a shorcode with an extensive PHP code?

1

How do I create a shortcode with this code here?  When I load and put the category in the post it loads the groups and attributes.  It is a Profit and Loss worksheet based on category, attributes, and groups.  The goal is to create a shortcode to be loaded into a search form, where the form creates a new post and sends it to database with a value of plus or minus 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

that is in the full 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>
    
asked by anonymous 13.05.2017 / 13:47

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 code in PHP is to use the heredoc syntax. See:

14.05.2017 / 04:43