在用户注册时创建帖子

时间:2012-05-25 作者:user983248

我如何在用户完成注册后创建3篇帖子?

理想的结果是使用用户作为帖子的作者并使用一些预定义的值创建3篇帖子。(帖子标题和slug)

帖子标题和slug将根据用户在注册表上提供的信息创建。

例如:

用户注册并提供姓名、姓氏、电子邮件(所有字段均为必填字段)将创建三个帖子,用户将是每个帖子的作者。每个帖子的标题中都会有注册表格中的slug信息

在上面的示例中,似乎没有必要编辑slug,但会出现带有“In”、“is”、“at”、“and”等字样的帖子,否则将被删除。还请注意,“预定义值将是:Bio、Portfolio和Contact。

它将接近wordpress在安装后创建的“欢迎”帖子。

知道怎么做吗?

我知道我需要使用wp_insert_post( $my_post ); 而且可能add_action(\'user_register\', \'some_function\'); 但我不知道如何使用wp_insert_post( $my_post ); 以及在用户完成注册过程后如何触发。

你知道如何做到这一点吗?提前谢谢。

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

你已经自己回答了这个问题,

创建一个将创建3个帖子的函数,例如:

function create_new_user_posts($user_id){
        if (!$user_id>0)
                return;
        //here we know the user has been created so to create 
        //3 posts we call wp_insert_post 3 times.
        // Create post object
        $my_bio_post = array(
             \'post_title\' => \'bio\',
             \'post_content\' => \'This is my post.\',
             \'post_status\' => \'publish\',
             \'post_author\' => $user_id
        );

        // Insert the post into the database
        $bio = wp_insert_post( $my_bio_post );
        $my_portfolio_post = array(
             \'post_title\' => \'portfolio\',
             \'post_content\' => \'This is my post.\',
             \'post_status\' => \'publish\',
             \'post_author\' => $user_id
        );

        // Insert the post into the database
        $portfolio = wp_insert_post( $my_portfolio_post );
        $my_contact_post = array(
             \'post_title\' => \'bio\',
             \'post_content\' => \'This is my post.\',
             \'post_status\' => \'publish\',
             \'post_author\' => $user_id
        );

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

        //and if you want to store the post ids in 
        //the user meta then simply use update_user_meta
        update_user_meta($user_id,\'_bio_post\',$bio);
        update_user_meta($user_id,\'_portfolio_post\',$portfolio);
        update_user_meta($user_id,\'_contact_post\',$contact);
}
然后使用user_register

add_action(\'user_register\',\'create_new_user_posts\');
Update当您将函数挂接到user_register 该函数接收用户id,以便您可以使用该id获取有关该用户的任何信息,例如:

$user = get_user_by(\'id\', $user_id);
现在$user是一个用户对象,因此您可以将帖子标题更改为user用户信息,例如:

\'post_title\' => $user->user_firstname . " ". $user->user_lastname . \'bio\'

结束

相关推荐

WordPress Posts By Date/Day?

我想在主页上输出最近3天的帖子。像这样:2012年3月30日 post 1 post 2 post 3 2012年3月29日 post 1 post 2 post 3 2012年3月28日 post 1 post 2 post 3 我试过:2012年3月30日,我有5个职位2012年3月29日,我有4个职位2012年3月28日,我有3个职位因此,第三天缺少2个帖子