WP_INSERT_POST允许用户无需登录即可发布...Broker

时间:2010-10-19 作者:andrewk

此问题已在stackoverflow上得到回答。这里有一个链接https://stackoverflow.com/questions/4321914/wp-insert-post-with-a-form/4321975#4321975

我试图让用户通过使用wp_insert_post() 作用

<?php $postTitle = $_POST[\'post_title\'];
    $post = $_POST[\'post\'];
    $submit = $_POST[\'submit\'];

    if(isset($submit)){

        global $user_ID;

        $new_post = array(
            \'post_title\' => $postTitle,
            \'post_content\' => $post,
            \'post_status\' => \'publish\',
            \'post_date\' => date(\'Y-m-d H:i:s\'),
            \'post_author\' => $user_ID,
            \'post_type\' => \'post\',
            \'post_category\' => array(7,100)
        );

        wp_insert_post($new_post);

    }

?>
我把它连接到分类页上的表格上

<form method="post" action=""> 
<input type="text" name="post_title" size="45" id="input-title"/>

<textarea rows="5" name="post" cols="66" id="text-desc"></textarea> 


<input type="hidden" name="cat" value="7,100"/> 

<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
我不知道我做错了什么。。它不起作用了。有什么想法吗?谢谢

2 个回复
SO网友:Denis de Bernardy

如果内存可用,wp\\u insert\\u post()会在多个点使用当前用户。

因此,您需要使用wp\\u set\\u current\\u user()将其切换到某个共享作者用户,然后在完成后将其切换回原始值。

或者,要求用户登录并允许所有组创建草稿。

SO网友:chris

你的一个问题是

if(isset($submit)){
您在上面的行上声明$submit

$submit = $_POST[\'submit\'];
这就是为什么

isset($submit)
将始终返回TRUE并执行代码。

我想补充一下

global $user_ID;
if ( $user_ID )
{
     //insert your post
}
else
{
     //give that person a message "Dude, you have to sign up or login to be able to submit content..."
}
然后跳过post\\u日期,并使用您的值键入post\\u,这些值无论如何都会被添加。你传递给那个函数的次数越少,你犯的错误就越少。。。

我会尝试将此添加到您的例如功能中。php脚本

$new_post = array(
            \'post_title\' => \'Test Post Title\',
            \'post_content\' => \'Test Post Content\',
            \'post_status\' => \'publish\',
            \'post_author\' => $user_ID,

        );
wp_insert_post($new_post);
这应该插入一篇带有gioven值的新闻帖子。。。如果这样做有效,您就知道必须在表单中查找错误。虽然我认为你不能将目录传递给wp\\U insert\\U post。。。。但首先尝试一下简单的测试。

结束

相关推荐

是否在URI请求参数表单中使用联合/交集查询_POSTS变量?

通常,在创建某种形式的查询时,我会对参数使用一个数组,如下所示: $postslistArgs = array( \'child_of\' => 320, \'parent\' => 320 ); $postslist = get_pages($postslistArgs);但是,在其他情况下,我还需要/想要使用URI样式的查询参数,如下所示:get_pages(\'child_of=