【发布时间】:2020-11-24 12:45:44
【问题描述】:
我想在用户注册时自动创建帖子。我使用下面的代码,效果很好。唯一缺少的是帖子作者必须是创建注册的用户。
有人可以帮我添加一行以使帖子的作者成为当前用户吗?谢谢!
这是代码:
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' => 'Team', // <- change to your cpt
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
【问题讨论】:
标签: php wordpress user-registration author