如何在注册时以用户的名字和姓氏作为标题创建新帖子

时间:2019-02-27 作者:Dobbs

我已经在这方面工作了一段时间了,我一辈子都搞不懂这一点。这是我目前的代码。

add_action(\'user_register\',\'create_new_user_profile\', 999);

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

    $user_info = get_userdata($user_id);
    $profile_full_name = $user_info->first_name .  " " . $user_info->last_name;

    $profile_post = array(
         \'post_title\' => $profile_full_name,
         \'post_content\' => \'This is the profile post.\',
         \'post_status\' => \'publish\',
         \'post_type\'    => \'Team\',
         \'post_author\' => $user_id
    );

    $profile = wp_insert_post( $profile_post );
}

我尝试了几种方法并搜索了一段时间,当前代码返回了一个空字符串,但通过其他替代方法,我可以发布用户名或user\\u id作为标题,但我无法获得display\\u名称或我需要的名字和姓氏的组合。

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

这个get_userdata 功能是您需要的。因此,对于display\\u名称

$user_info = get_userdata($user_id);
$profile_display_name = $user_info->display_name;
至于名字和姓氏

$full_name = $user_info->first_name .  " " . $user_info->last_name;

相关推荐

从PHP 5.6更改为PHP 7.2时出现问题

我有一个无法更新的旧主题问题。我的主机(1和1)现在正在收取继续使用PHP 5.6的费用,但我在切换到PHP 7.2时遇到以下错误,我不知道该怎么办。如果有人能帮忙,我们将不胜感激。提前谢谢。Warning: Declaration of mysiteDescriptionWalker::start_el(&$output, $item, $depth, $args) should be compatible with Walker_Nav_Menu::start_el(&$output,