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.

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 im doing some sort of schoolboy error but it just wont 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 thats not working, it just pushes you back to the first form and doesnt post the data.

Any help at all would be appreciated.

    <?php
/*
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">




<?php
/* 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">


    </form>


<?php
    }
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);
    curl_close($ch);


    return $data;
}


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


//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$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' ); ?>


</form>
<?php
}
?>




<?php
/* 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
    );

    //SAVE THE POST
    $pid = wp_insert_post($new_post);

             //SET OUR TAGS UP PROPERLY
    wp_set_post_tags($pid, $_POST['_tags']);

    //REDIRECT TO THE NEW POST ON SAVE
    $link = get_permalink( $pid );
    wp_redirect( $link );

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM

//POST THE POST YO
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(); ?>
share|improve this question
    
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... –  Stephen Harris Jan 27 '12 at 14:46

1 Answer 1

up vote 0 down vote accepted

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:

$nonce=$_REQUEST['_wpnonce'];
if (! wp_verify_nonce($nonce, 'new-post') ) die('Well that was naughty.');

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

'POST' == $_SERVER['REQUEST_METHOD']

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.

share|improve this answer
    
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 '12 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 '12 at 19:22

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.