长期潜伏者,第一次海报,大人物PHP noob。
如果我的问题有点含糊,我深表歉意,但我有下面的代码,我知道我犯了一些小学生的错误,但它只是不会将表单内容发送到帖子。
基本的输出行是一个表单,您提交一个URL,然后另一个表单显示其中的元数据,然后该表单被提交到WordPress帖子。这最后一点不起作用,它只是将您推回到第一个表单,而不发布数据。
任何帮助都将不胜感激。
<?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(); ?>
最合适的回答,由SO网友:Stephen Harris 整理而成
请参见我上面的评论,但部分问题在于您正在检查
$_POST[\'action\'] == "new_post"
在您给出的示例中
commented out the hidden \'action\' field; 在任何情况下,设置为
post
不
new-post
. 我相信你是想检查一下现在的情况。。。
为此(遵循Codex), 一旦您检查了action, 执行:
$nonce=$_REQUEST[\'_wpnonce\'];
if (! wp_verify_nonce($nonce, \'new-post\') ) die(\'Well that was naughty.\');
此外,我认为没有必要检查:
\'POST\' == $_SERVER[\'REQUEST_METHOD\']
最后,在使用之前
wp_insert_post
. 您应该检查类别是否已设置(也请参见上面的注释)。你可能应该
intval
将已发布的类别数据作为数组插入
wp_insert_post
参数。