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.

Maybe someone can help me with a small problem I'm having. I'm trying to use this: http://jquery.malsup.com/form/ to create a submit form for Wordpress. I've tested it on a regular site(not WP) and it works. I can't figure out why it won't work in wp.

This is my form. I've created two page templates, one has the form, the other has the wp_insert_post code. If I change the form action to action="http://mysite.com/page.php" it works, if I point it to a page template it doesn't. I need it to point to a page template for the wp_insert_post code to work.

This is on one first page:

<form id="myForm" action="http://mysite.com/submitest" method="post"> 
Name: <input type="text" name="name" /> 
Comment: <textarea name="comment"></textarea> 
<input type="submit" value="Submit Comment" /> 

Thanks

share|improve this question
    
Do you need the second template at all? I mean are you showing anything regarding the processing of this form on the second template? I think its not a very good idea to put your post insertion code (or any heavy processing code, for that matter) in a page template. functions.php is the RIGHT place for putting your post insertion code. You could get the form data, insert the post, and redirect to the page that has the second template applied. –  Rutwick Gangurde Nov 16 '11 at 15:18
    
that's how the example that I downloaded from here looked like. Two pages one with the form and the other with the processing code. How do I get the form data in functions.php? –  ciprian Nov 16 '11 at 15:22
    
See my answer... –  Rutwick Gangurde Nov 16 '11 at 15:22
add comment

1 Answer 1

up vote 2 down vote accepted

Leave the action value as blank. Give the submit button a name as name='form_sub'. Then add the following code to your functions.php file. Check the codex for init hook. You can create a 'Thank You' page, or a confirmation page where the user should go after successful submission.

<?php
add_action('init', 'form_submit');

function form_submit(){
    if(isset($_POST['form_sub']))
    {
        //here you'll have your form data in a $_POST array, you can check it using a print_r. parse the form and insert the post
        $title = $_POST['name'];
        $content = $_POST['comment'];

        //change the category and author as you want
        $post_obj = array(
                        'post_title' => $title,
                        'post_content' => $content,
                        'post_category' => array(1), //Uncategorized
                        'post_status' => 'draft',
                        'post_author' => 1 //Admin
                       );
        $id = wp_insert_post($post_obj);

        //check if successfully submitted
        if(isset($id) && !is_wp_error($id))
        {
            //redirect to a thank you page, make sure the slug is 'thank-you'
            wp_redirect(home_url('/thank-you'));
            exit;
        } 
    }
}
?>

Try this. Let me know if you are stuck.

Edit: This is how the form template should be. The submit button should have the name attribute, not the form.

<?php /* Template Name: Submit */ ?> 
<?php get_header(); ?> 
Form this: 
<form action="" method="post"> 
    Name: <input type="text" name="name" /> 
    Comment: <textarea name="comment"></textarea>
    <input type="submit" value="Submit" name="form_sub" />
</form>
<?php get_footer(); ?>
share|improve this answer
    
Check the code! –  Rutwick Gangurde Nov 16 '11 at 16:00
    
Thanks for your help. I tried a few times but nothing is submitting. I ll do a few more tests and let you know. –  ciprian Nov 16 '11 at 18:16
    
It's still not working. This is what I have on my template page <?php /* Template Name: Submit */ ?> <?php get_header(); ?> Form this <form action="" name="form_sub" method="post"> Name: <input type="text" name="name" /> Comment: <textarea name="comment"></textarea> <input type="submit" value="Submit" /> </form> <?php get_footer(); ?> I checked everything and can't seem to find where the mistake is. –  ciprian Nov 16 '11 at 19:01
    
btw, how do you post your code? I add the two `` but my comment doesn't look like yours. –  ciprian Nov 16 '11 at 19:02
    
Enter twice, leave 4 spaces (that's a tab in a normal editor) and start typing your code. Keep the 4 spaces consistent throughout your code. Backticks are good only for single lines of code. Note: This formatting will work only for an answer, not a comment. –  Rutwick Gangurde Nov 17 '11 at 3:30
show 17 more comments

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.