Long time lurker, first time poster, and big time PHP noob.

Apologies if my question is a little vague, but I have the code below and I know I'm doing some sort of schoolboy error but it just won't send the form contents to a post.

The basic out line is one form, you submit a URL, then another form appears with meta data in it, then that form gets submitted to a WordPress post. Its this last bit that's not working, it just pushes you back to the first form and doesn't post the data.

Any help at all would be appreciated.

Template Name: page-url

<?php get_header(); ?>

<?php get_template_part('includes/breadcrumbs'); ?>

<div id="content" class="clearfix fullwidth">

<div id="container">
<div id="content" role="main">

/* if the "submit" variable does not exist, the form has not been submitted - display initial page */
if (!isset($_POST['submit'])) {


    <form action="" method="post">
    Enter your url: <input name="formuserurl">
    <input type="submit" name="submit" value="Go">


else {
/* if the "submit" variable exists, the form has been submitted - look for and process form data */

function file_get_contents_curl($url)
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);

    return $data;

$enteredurl = $_POST['formuserurl'];
$html = file_get_contents_curl($enteredurl);

//parsing begins here:
$doc = new DOMDocument();
$nodes = $doc->getElementsByTagName('title');

//get and display what you need:
$title = $nodes->item(0)->nodeValue;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
    if($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');


<form action="" method="post">
Url: <input name="_url2" value="<?php echo $enteredurl; ?>" id="">
<br />
Title: <input name="_title" value="<?php echo $title; ?>" id="">
<br />
Tags: <input name="_tags" value="<?php echo $keywords; ?>" id="">
<br />
Description: <textarea name="_description" cols="" rows="" value="" id=""><?php echo $description; ?></textarea>
<br />
Category: <?php wp_dropdown_categories('tab_index=10&taxonomy=category&hide_empty=0'  ); ?>
<br />
<input type="submit" name="submitpost" value="Go">

<!--<input type="hidden" name="post_type" id="post_type" value="post" />

<input type="hidden" name="action" value="post" />

<?php wp_nonce_field( 'new-post' ); ?>


/* This is to post the form content to wordpress*/

if (!isset($_POST['submitpost'])) {

else {
/* This is to post the form content to worpdress*/

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

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

    $tags = $_POST['_tags'];

    // ADD THE FORM INPUT TO $new_post ARRAY
    $new_post = array(
    'post_title'    =>  $title,
    'post_content'  =>  $description,
    'post_category' =>  array($_POST['_categories']),  // Usable for custom taxonomies too
    'tags_input'    =>  array($tags),
    'post_status'   =>  'publish',           // Choose: publish, preview, future, draft, etc.
    'post_type' =>  'post'  //'post',page' or use a custom post type if you want to

    $pid = wp_insert_post($new_post);

    wp_set_post_tags($pid, $_POST['_tags']);

    $link = get_permalink( $pid );
    wp_redirect( $link );


do_action('wp_insert_post', 'wp_insert_post');



</div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

</div> <!-- #content -->

<div id="content-bottom-bg"></div>

<?php get_footer(); ?>
  • Probably not causing the problem but you can't use wp_redirect that late and $_POST['_categories'] should be just $_POST['categories']. Finally, by your logic after the second submission the original form will always display. Can't quite see why the post is not being created at the moment... Jan 27, 2012 at 14:46

1 Answer 1


See my comments above, but part of the problem is that you are checking that

$_POST['action'] == "new_post"

In the example you've given you have commented out the hidden 'action' field; which, in any case is set to post not new-post. I believe you a trying to check the nonce...

To do that (following the Codex), once you've checked the action, do:

if (! wp_verify_nonce($nonce, 'new-post') ) die('Well that was naughty.');

Also, I don't think it's necessary to check:


Finally, before using wp_insert_post. You should check that the category is set (see above comments too). You should probably intval the posted category data before inserting as an array into the wp_insert_post arguments.

  • Thanks for the answer, something to look into certainly. As I said im still feeling my way around PHP. I think i will need to study the intval a bit more as well. Havent managed to get it to post yet though
    – Dean
    Jan 27, 2012 at 18:41
  • I ended up rejigging the code into a chain of 3 pages and changing the code that posts to wordpress, got it working. Thanks for your help!
    – Dean
    Jan 30, 2012 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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