0

I'm trying to put my frontend form into a variable or a shortcode so I can wrap another shortcode around it (specifically Bainternet Post Creation Limits).

My form has PHP code in it so I'm not exactly sure how to do this. I have tried creating other variables for each line of PHP code but it's not working as I thought it might.

I know how to create a shortcode; I don't know how to put a form with PHP into a shortcode though. I don't know how to put the form into a variable at all and I can't find information how to do this.

Here's the form:

<form id="new_post" name="new_post" method="post" action="" class="submit-form" enctype="multipart/form-data">
    <fieldset class="tax">
        <label for="class">Class Price:</label>
            <input type="text" value="" class="class" id="class" name="class" placeholder="20" /> &euro;
        <label for="accent">Accent:</label>
            <?php wp_dropdown_categories( 'tab_index=15&taxonomy=accent&hide_empty=0&name=accent&show_option_all=Select one' ); ?>
        <h2>Rates</h2>
        <div class="teachratebox">
        <label for="presencial">Face-to-face classes</label>
        <?php
            $clocations = get_terms('class-location', 'orderby=id&hide_empty=0');
            $counter = 0;
            foreach ($clocations as $clocation) {
                $counter++;
                $option = '<div class="slots"><input class="checkbox" type="checkbox" name="class-location[]" id="'.$clocation->slug.'" value="'.$clocation->slug.'" onclick="enable_text(this.checked)" />';
                $option .= '<span class="clocation" for="clocations">'.$clocation->name.'</span>';
                $option .= '<input type="text" class="rate" value="" id="'.$clocation->slug.'-rate" name="'.$clocation->slug.'-rate" disabled="disabled" /> &euro;</div>';
                echo $option;
            }
        ?>
        </div>

        <input type="submit" value="Add Teacher" tabindex="40" id="psubmit" name="submit" />
    </fieldset>

        <input type="hidden" name="action" value="new_post" />
        <?php wp_nonce_field( 'new-post' ); ?>

</form>

How do I put this form into a variable or a shortcode?

2 Answers 2

1

I would suggest that you remove the php from the form and instead write out the relevant HTML in your shortcode code.

  • Loop through wp_dropdown_categories and create a dropdown

  • Loop through clocations and build your collection of checkboxes

  • Drop the wp_nonce_field and code up something simple yourself using a hidden field.

Essentially the code doesn't change much, it just outputs to a string instead. Once your shortcode puts out pure HTML, it can be wrapped inside another.

1
  • I took a different, simpler approach which basically involved a conditional check in the template before the form rather that using a plugin plus having to rewrite the form. Thanks anyway. Commented Mar 19, 2013 at 18:08
1

Beside the fact that your code could be better, here is a simple solution. Using output buffers will write everything to a variable.

<?php
ob_start();
?>
<form id="new_post" name="new_post" method="post" action="" class="submit-form" enctype="multipart/form-data">
<fieldset class="tax">
    <label for="class">Class Price:</label>
        <input type="text" value="" class="class" id="class" name="class" placeholder="20" /> &euro;
    <label for="accent">Accent:</label>
        <?php wp_dropdown_categories( 'tab_index=15&taxonomy=accent&hide_empty=0&name=accent&show_option_all=Select one' ); ?>
    <h2>Rates</h2>
    <div class="teachratebox">
    <label for="presencial">Face-to-face classes</label>
    <?php
        $clocations = get_terms('class-location', 'orderby=id&hide_empty=0');
        $counter = 0;
        foreach ($clocations as $clocation) {
            $counter++;
            $option = '<div class="slots"><input class="checkbox" type="checkbox" name="class-location[]" id="'.$clocation->slug.'" value="'.$clocation->slug.'" onclick="enable_text(this.checked)" />';
            $option .= '<span class="clocation" for="clocations">'.$clocation->name.'</span>';
            $option .= '<input type="text" class="rate" value="" id="'.$clocation->slug.'-rate" name="'.$clocation->slug.'-rate" disabled="disabled" /> &euro;</div>';
            echo $option;
        }
    ?>
    </div>

    <input type="submit" value="Add Teacher" tabindex="40" id="psubmit" name="submit" />
</fieldset>

    <input type="hidden" name="action" value="new_post" />
    <?php wp_nonce_field( 'new-post' ); ?>

</form>
<?php
$output = ob_get_contents();
ob_end_clean();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.