我已经处理了已经发送的标题的各种问题,检查了是否有空格,并确保在wp\\u重定向之后出现get\\u标题行,但我被卡住了。
代码为:
<?php if ( !is_user_logged_in()) {
include (TEMPLATEPATH . \'/member.php\');
} else { ?>
<?php
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 a title\';
$tags = $_POST[\'post_tags\'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_category\' => array($_POST[\'cat\']), // Usable for custom taxonomies too
\'tags_input\' => array($tags),
\'post_status\' => \'draft\', // 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);
//KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
wp_set_post_tags($pid, $_POST[\'post_tags\']);
//REDIRECT TO THE NEW POST ON SAVE
$link = include (TEMPLATEPATH . \'/thankyou.php\');
wp_redirect( $link );
//INSERT OUR MEDIA ATTACHMENTS
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
}
} // END THE IF STATEMENT FOR FILES
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
//POST THE POST YO
do_action(\'wp_insert_post\', \'wp_insert_post\');
?>
<?php get_header(); ?>
<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="post">
<div class="title">
<h2>Post</h2>
</div>
<div class="form-content">
<div>
<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
<fieldset name="name">
<label for="title">Title:</label>
<input type="text" id="title" value="" tabindex="5" name="title" />
</fieldset>
<fieldset class="tax">
<label for="cat">Category:</label>
<?php wp_dropdown_categories( \'tab_index=10&taxonomy=type&hide_empty=0\' ); ?>
</fieldset>
<fieldset class="content">
<label for="description">Description:</label>
<textarea id="description" tabindex="15" name="description" cols="60" rows="10"></textarea>
</fieldset>
<fieldset class="images">
<label for="company_logo">Company Logo</label>
<input type="file" name="company_logo" id="company_logo" tabindex="25" />
</fieldset>
<fieldset class="submit">
<input type="submit" value="Post It" tabindex="40" id="psubmit" name="submit" />
</fieldset>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
</div>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php } ?> <!-- user is logged in -->
如果帖子状态设置为draft,并重定向到thankyou。php,我得到的标题已经发送消息。帖子被保存了下来,谢谢你。显示php页面,但显示已发送消息的标题。
如果我将post状态设置为publish,并将重定向设置为类似以下内容:
$link = get_permalink( $pid );
wp_redirect( $link );
exit;
然后,表单可以顺利工作,并显示已发布的帖子。
我希望能够在帖子发布之前对其进行审阅,这就是为什么我希望将状态发送到草稿,并在海报上显示消息。
我如何做到这一点?
调试信息:
Notice: Undefined index: post_tags in C:\\xampp\\htdocs\\test\\wp-content\\themes\\testtheme\\postform.php on line 31
Notice: Undefined variable: description in C:\\xampp\\htdocs\\test\\wp-content\\themes\\testtheme\\postform.php on line 48
Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\test\\wp-includes\\post.php on line 2915
Notice: Undefined index: mytheme_meta_box_nonce in C:\\xampp\\htdocs\\test\\wp-content\\themes\\testtheme\\lib\\metabox.php on line 177
SO网友:Rajeev Vyas
Use init hook...
<?php
function custom12345_wp_insert_post()
{
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 a title\';
$tags = $_POST[\'post_tags\'];
// ADD THE FORM INPUT TO $new_post ARRAY
$new_post = array(
\'post_title\' => $title,
\'post_content\' => $description,
\'post_category\' => array($_POST[\'cat\']), // Usable for custom taxonomies too
\'tags_input\' => array($tags),
\'post_status\' => \'draft\', // 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);
//KEEPS OUR COMMA SEPARATED TAGS AS INDIVIDUAL
wp_set_post_tags($pid, $_POST[\'post_tags\']);
//INSERT OUR MEDIA ATTACHMENTS
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
}
} // END THE IF STATEMENT FOR FILES
//REDIRECT TO THE NEW POST ON SAVE
$link = include (TEMPLATEPATH . \'/thankyou.php\');
wp_redirect( $link );
exit;
} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM
}
}
add_action(\'init\',\'custom12345_wp_insert_post\');
?>
<?php if ( !is_user_logged_in()) {
include (TEMPLATEPATH . \'/member.php\');
} else { ?>
<?php get_header(); ?>
<div id="content" role="main">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="post">
<div class="title">
<h2>Post</h2>
</div>
<div class="form-content">
<div>
<form id="new_post" name="new_post" method="post" action="" enctype="multipart/form-data">
<fieldset name="name">
<label for="title">Title:</label>
<input type="text" id="title" value="" tabindex="5" name="title" />
</fieldset>
<fieldset class="tax">
<label for="cat">Category:</label>
<?php wp_dropdown_categories( \'tab_index=10&taxonomy=type&hide_empty=0\' ); ?>
</fieldset>
<fieldset class="content">
<label for="description">Description:</label>
<textarea id="description" tabindex="15" name="description" cols="60" rows="10"></textarea>
</fieldset>
<fieldset class="images">
<label for="company_logo">Company Logo</label>
<input type="file" name="company_logo" id="company_logo" tabindex="25" />
</fieldset>
<fieldset class="submit">
<input type="submit" value="Post It" tabindex="40" id="psubmit" name="submit" />
</fieldset>
<input type="hidden" name="action" value="new_post" />
<?php wp_nonce_field( \'new-post\' ); ?>
</form>
</div>
</div><!-- .entry-content -->
</div><!-- #post-## -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php } ?> <!-- user is logged in -->