2

我创建了一个网站,运动员可以通过该网站注册并创建自己的模板化个人资料页面。注册时,我会自动创建一个自定义帖子(他们的个人资料)并将用户设置为该帖子的作者。

但是,我不能终生将帖子标题设置为用户(现在是作者)的名字和姓氏。例如,如果John Smith注册,我希望帖子标题为John Smith,并将 slug 转换为player/john.smith

$user_info->nickname现在已经使用过,但这会导致标题和 slug 都读为john.smith

这是我正在使用的代码 - 任何指针都将不胜感激:

add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    if ($this_user_role == 'author') {

        // Create a new post
        $user_post = array(
            'post_title'   => $user_info->nickname,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}
4

3 回答 3

2

您还应该使用 post_name 作为 slug(john.smith) 和 first_name,last name 作为帖子标题,如下所示:

add_action( 'user_register', 'wpse_216921_company_cpt', 20, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    if ($this_user_role == 'author') {
        
        $post_title = $user_info->first_name.' '.$user_info->last_name;
        $post_title = trim(ucwords($post_title));
        $post_slug  = preg_replace('/\s+/', '.', $post_title);

        // Create a new post
        $user_post = array(
            'post_title'   => $post_title, //$user_info->display_name,
            'post_name'    => $post_slug,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}
于 2021-10-12T04:45:50.390 回答
1

您可以使用名字和姓氏,如下所示:

add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );

function wpse_216921_company_cpt( $user_id )
{
    // Get user info
    $user_info = get_userdata( $user_id );
    $user_roles = $user_info->roles;

    // New code added 
    $this_user_role = implode(', ', $user_roles );

    $first_name = $user_info->first_name;
    $last_name = $user_info->last_name;

    if ($this_user_role == 'author') {

        // Create a new post
        $user_post = array(
            'post_title'   => $first_name . ' ' . $last_name,
            'post_status'  => 'publish', // <- here is to publish
            'post_type'    => 'athlete', // <- change to your cpt
            'post_author'  => $user_info->ID
        );
        // Insert the post into the database
        $post_id = wp_insert_post( $user_post );
    }
}

未经测试,但它应该可以工作。

于 2021-10-08T11:06:31.123 回答
0

谢谢各位帮忙。原来是 Ultimate Member 问题,我需要使用 UM Hook um_registration_complete。

最终的解决方案如下:

//Create Custom Post Type on registration

add_action( 'um_registration_complete', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
   // Get user info
   $user_info = get_userdata( $user_id );
   $user_roles = $user_info->roles;

   // New code added
   $this_user_role = implode(', ', $user_roles );

   if ($this_user_role == 'author') {

   $post_title = $user_info->first_name.' '.$user_info->last_name;
   $post_title = trim(ucwords($post_title));
   $post_slug = preg_replace('/\s+/', '.', $post_title);

   // Create a new post
   $user_post = array(
   'post_title' => $post_title, //$user_info->display_name,
   'post_name' => $post_slug,
   'post_status' => 'publish', // <- here is to publish
   'post_type' => 'athlete', // <- change to your cpt
   'post_author' => $user_info->ID
   );
   // Insert the post into the database
   $post_id = wp_insert_post( $user_post );
   }
}
于 2021-10-14T08:26:30.787 回答