Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

share|improve this answer
    
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. –  annabwashere Mar 19 '13 at 18:08

Use

function shortcode_fn() {
?>
<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
}

add_shortcode('shortcode', 'shortcode_fn');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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