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.
<?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(); ?>
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.
<?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
edited Aug 24, 2019 at 17:34
Lazar Momcilovic
1831 silver badge6 bronze badges
asked Jan 27, 2012 at 14:31
DeanDean
32 bronze badges
1
|
1 Answer
Reset to default 0See 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745218580a4617140.html
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 Commented Jan 27, 2012 at 14:46