未注册用户在WordPress中提交帖子

时间:2012-07-08 作者:hd.

我是wordpress的新手。是否可以允许未注册用户提交帖子并将其放入队列,以便与站点管理员确认以及何时显示在站点上?

2 个回复
SO网友:Pontus Abrahamsson

UPDATE - 我为此制作了一个简单的插件,添加了以下功能:

一个小部件,您可以将其放在侧边栏中,使post to bee保存为pendling插件有一些简单的css类,没有任何样式。

Here is the download link: Simple Frontend post

The function and form:

要使其正常工作,可以使用函数wp_insert_post 要将帖子/页面插入数据库,它会清理变量,进行一些检查,填写缺少的变量,如日期/时间等。

首先,您需要一个表单来获取所需的所有必要内容。

这是一个启用标题和文本内容的表单,请将其放置在表单所需的位置:

<form action="<?php echo site_url(); ?>/" method="post">
    <input type="text" id="title" value="" tabindex="5" name="title" />
    <textarea tabindex="3" name="desc" cols="5" rows="3"></textarea>
    <input type="submit" value="Submit" name="frontendpost">
</form>
这是一个将所有内容放入数据库并将其保存为pendling的函数,也就是说,你必须先确认它,然后才能在前面看到它,将其放入你的funtions.php theme file:

<?php
    //Enable the front-end postings
    function save_frontend_post() {

        if ( !empty( $_POST ) && isset( $_POST[\'frontendpost\'] ) ) {

            //Check to make sure that the post title field is not empty
            if( trim( $_POST[\'title\'] ) === \'\' ) {
                $error = true;
            } else {
               $title = trim( $_POST[\'title\'] );
            }

            //Check to make sure sure that content is submitted
            if( trim( $_POST[\'desc\'] ) === \'\' )  {
                $error = true;
            } else {
               $desc = trim( $_POST[\'desc\'] );
            }

            //If there is no error, send the form
            if( !isset( $error ) ) {

                //Create post object
                $new_post = array(
                    \'post_title\'     => $title,              //The title of your post.
                    \'post_content\'   => $desc,               //The full text of the post.
                    \'post_date\'      => date(\'Y-m-d H:i:s\'), //The time post was made.
                    \'post_status\'    => \'pending\',           //Set the status of the new post.
                    \'post_type\'      => \'post\'
                );

                //Insert the post into the database
                $new_post = wp_insert_post( $new_post );        

            }
        }
    }
    add_action( \'wp_head\', \'save_frontend_post\', 10, 2 );
?>

结束