我创建了一个网站,运动员可以在这里注册并创建自己的模板个人资料页面。在注册时,我自动创建一个自定义帖子(他们的个人资料)并将用户设置为该帖子的作者。
然而,我不能对我的生活将文章标题设置为用户的(现在的作者)的名字和姓。例如,如果John Smith注册,我希望帖子标题为John Smith,并将标签转换为athlete/John . Smith。
我现在使用的是$user_info->nickname
,但这会导致标题和段符读为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 );
}
}
您还应该使用post_name作为slug(john.smith),并使用first_name作为post title,就像这样:
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 );
}
}
你可以像下面这样使用姓和名:
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 );
}
}
未测试,但应该可以工作。
感谢大家的帮助。这原来是一个终极会员问题,我需要使用UM钩子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 );
}
}