使用USER_REGISTER创建帖子不会创建标题

时间:2013-03-06 作者:Ben Racicot

我已经获得了用户注册,可以使用user\\u register挂钩自动创建一篇帖子,但是标题显示为(没有标题),有人能帮我解决我这里的错误吗?

add_action(\'user_register\',\'create_new_user_post\');

$userinfo = get_userdata($user_id);
$disname = $userinfo->display_name;

function create_new_user_post($user_id){
        if (!$user_id>0)return;

        // Create post object
        $my_bio_post = array(
           // \'post_type\' => \'ggs_user_post_type\',
             \'post_title\' => $disname,
             \'post_content\' => \'Put cool content here!\',
             \'post_status\' => \'publish\',
             \'post_author\' => $user_id
        );

        // Insert the post into the database
        $bio = wp_insert_post( $my_bio_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);
}

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

您的功能create_new_user_post() 无法看到:

$userinfo = get_userdata($user_id);
$disname = $userinfo->display_name;
将这些放在函数中,以便函数可以访问这些变量。

SO网友:s_ha_dum

$disname 未设置。您可以在函数的scope. 这同样适用于$userinfo. 而你没有$user_id 设置其中一个。它通过add_action 机械装置正如Josh所说,您需要将这两个函数都拉入回调函数(并选择他的答案。他先到了这里,答案是正确的,只是不太详细。考虑一下这个“补充”。)

function create_new_user_post($user_id){
    if (!$user_id>0) return;

    $userinfo = get_userdata($user_id);
    $disname = $userinfo->display_name;
    // and the rest

结束

相关推荐