0

As always, I have unique situations and client requests. I am working on a custom dashboard widget that will include a form to register for an event. Once the form is submitted, it will generate a custom post type of invoice.

Currently, I'm just playing with the basics to get things working. So here is my current code:

function InvoiceRegisterContent() {

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "add_new_post") {

    $new_post = array(
          'ID' => '',
          'post_author' => $user->ID, 
          'post_title' => $post_title,
          'post_status' => 'publish'
        );

    $post_id = wp_insert_post($new_post);

    // This will redirect you to the newly created post
    $link = get_admin_url();
    wp_redirect( $link );

}

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="" />
        <input type="hidden" name="post_title" value="test" />
        <input type="hidden" name="post_type" value="post" />
        <input type="hidden" name="action" value="add_new_post" />
        <?php wp_nonce_field( 'add-post' ); ?>
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>

Now my problem is, I can a "headers already sent error" which I know is because of the form handling at the beginning of the script. Does anyone know the proper way to code the form handling so that it is done before headers are sent?

I didn't post the rest of the plugin code that generates the widget, because that's not where the problem is.

UPDATE

Here is the code I tried using admin_post. This is within a plugin I am building.

function add_new_post() {
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "add_new_post") {

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
} else {
        echo 'Please enter a  title';
}
if (isset ($_POST['description'])) {
        $description = $_POST['description'];
} else {
        echo 'Please enter the content';
}

// Add the content of the form to $post as an array
$new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' => 'post'  //'post',page' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
wp_redirect(admin_url()); 
//insert taxonomies
    }
}

add_action ( 'admin_post_add_new_post', 'InvoiceRegisterContent');

function InvoiceRegisterContent() {

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="" />
        <input type="hidden" name="title" value="test" />
        <input type="hidden" name="action" value="add_new_post" />
        <?php wp_nonce_field( 'add-post' ); ?>
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>

ANOTHER UPDATE

With the current code (which still doesn't work, when I click on the submit button, it opens a new page "admin-post.php" and shows the form again which is not functional.

function add_new_post() { if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == "add_new_post") {

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
} else {
        echo 'Please enter a  title';
}
if (isset ($_POST['description'])) {
        $description = $_POST['description'];
} else {
        echo 'Please enter the content';
}

// Add the content of the form to $post as an array
$new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' => 'post'  //'post',page' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
wp_redirect(admin_url()); 
//insert taxonomies
    }
}

add_action ( 'admin_post_add_new_post', 'InvoiceRegisterContent');

function InvoiceRegisterContent() {

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="http://mps.ras01.com/wp-admin/admin-post.php" />
        <input type="hidden" name="title" value="test" />
        <input type="hidden" name="action" value="add_new_post" />
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>
  • I would look at moving the form handling to a function that is hooked into the admin_post_{action} hook: codex.wordpress.org/Plugin_API/Action_Reference/ – czerspalace Nov 11 '15 at 19:10
  • hmm...I'm not having luck with that either. Here is what I implemented: add_action('admin_post_custom_function', 'call_to_form') – RiotAct Nov 11 '15 at 19:33
  • Can you update your question to show the code you have now? Did you change the action for the form? Where is all this code located? – czerspalace Nov 11 '15 at 19:46
  • i had a similar issue..and i solved following this topic – sb0k Nov 11 '15 at 20:19
  • I think I cut off the link I meant to post for codex.wordpress.org/Plugin_API/Action_Reference/…. You want to change action="http://example.com/wp-admin/admin-post.php" and a hidden field Called action that is set to add_new_post – czerspalace Nov 12 '15 at 5:10
1

Finally got this working! This is just a basic test to get the form working, but if anyone else is working on a similar issue, this is how I got it to work. Another important thing, to keep the "Headers already sent" error away, make sure that there are NO errors i.e. all fields are complete so no error message is sent back.

function add_new_post() {
if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "add_new_post") {

// Do some minor form validation to make sure there is content
if (isset ($_POST['title'])) {
        $title =  $_POST['title'];
} else {
        echo 'Please enter a  title';
}
if (isset ($_POST['description'])) {
        $description = $_POST['description'];
} else {
        echo 'Please enter the content';
}

// Add the content of the form to $post as an array
$new_post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'publish',           // Choose: publish, preview, future, draft, etc.
        'post_type' => 'post'  //'post',page' or use a custom post type if you want to
);
//save the new post
$pid = wp_insert_post($new_post);
wp_redirect(admin_url()); 
//insert taxonomies
    }
}

add_action ( 'init', 'add_new_post');

function InvoiceRegisterContent() {

?>
    <form id="new_post" class="form-content" name="new_post" method="post" action="" />
        <input type="hidden" name="title" value="test" />
        <input type="hidden" name="description" value="test" />
        <input type="hidden" name="action" value="add_new_post" />
        <input type="submit" value="Register" class="button" name="submit" />   
    </form>
<?php 
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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