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 am trying to create a nonce to use with a (public-facing) form.

Below is my code:

function my_form() {
    if (isset($_POST['submit'])) { 

    $name = $_POST['name'];
    $description = $_POST['description'];
    $output_form = false;

        if (wp_verify_nonce($_POST['added'], 'add-item') )
        {
        //validate
        echo 'form submitted with nonce correctly';
        }

    } else {
       $output_form = true;
    }
    if ($output_form) {
    ?>
    <form method="post" action="">
        <?php wp_nonce_field('add-item','added'); ?>
            <label>Title:</label><br/>
            <input type="text" name="name" value="<?php echo $name; ?>"/><br/>
            <label>Description:</label><br />
            <textarea name="description" rows="3" cols="10"><?php echo $description; ?></textarea><br/>
            <input type="submit" name="submit" value="submit">
        </form>
    <?php
    }   

}

add_shortcode('PODS FORM', my_form);

I can see my form on my page, but when I submit (and fill it our correctly) instead of seeing my 'correct' message I am only seeing blank page.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You need to pass the value of the nonce field as first argument to wp_verify_nonce. So, you need to modify the nonce verification part in your code.

Also you were using form fields names that conflicts with internal wordpress query vars, you should prefix them with something unique so they do not conflict with wordpress. See following example:

function my_form() {
    if (isset($_POST['pf_submit'])) { 

    $name = $_POST['pf_name'];
    $description = $_POST['pf_description'];
    $output_form = false;

        if (isset( $_POST['pf_added'] ) && wp_verify_nonce($_POST['pf_added'], 'add-item') )
        {
        //validate
        echo 'form submitted with nonce correctly';
        }

    } else {
       $output_form = true;
       $name = "";
       $description = "";
    }
    if ($output_form) {
    ?>
    <form method="post" action="">
        <?php wp_nonce_field('add-item','pf_added'); ?>
            <label>Title:</label><br/>
            <input type="text" name="pf_name" value="<?php echo $name; ?>"/><br/>
            <label>Description:</label><br />
            <textarea name="pf_description" rows="3" cols="10"><?php echo $description; ?></textarea><br/>
            <input type="submit" name="pf_submit" value="submit">
        </form>
    <?php
    }   

}

add_shortcode('PODS FORM', my_form);
share|improve this answer
    
Thanks! However, I added your code but I am still getting a "Page not found" error (changes to the code made above) –  redconservatory Jul 29 '11 at 17:00
    
still getting Page not found? You said in your question you are seeing the blank page. Blank page and page not found are two different issues. Kindly confirm if you are seeing the blank page or page not found. I just looked at the other parts of your code and they look buggy too. Give me few minutes and I will get back to you. –  Hameedullah Khan Jul 29 '11 at 17:11
    
I have updated my answer with the complete fixed code. –  Hameedullah Khan Jul 29 '11 at 17:19
    
Actually, this works great! The renaming helped! –  redconservatory Jul 29 '11 at 17:42
    
Just wondering...do I have to pass a difference nonce each time? –  redconservatory Jul 29 '11 at 17:43

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.