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
}
?>
admin_post_{action}
hook: codex.wordpress.org/Plugin_API/Action_Reference/add_action('admin_post_custom_function', 'call_to_form')
action="http://example.com/wp-admin/admin-post.php"
and a hidden field Called action that is set toadd_new_post