在用户注册wordpress后创建一个帖子



我需要在用户注册后创建一个帖子,在wordpress中扮演一些角色!

我用了这个代码

add_action( 'user_register', 'create_resume', 10, 1 );
function create_resume( $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 == 'candidate') {
// Create a new post
$user_post = array(
'post_title'   => $user_info->username,
'post_status'  => 'publish', // <- here is to publish
'post_type'    => 'resume', // <- change to your cpt
'post_author'  => '$Post_id',
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}

但不起作用!:-(

有人能帮我吗?

试试这个:

add_action( 'user_register', 'create_resume', 10, 1 );
function create_resume( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added 
$has_candidate_role = in_array('candidate', $user_roles);
if ($has_candidate_role) {
// Create a new post
$user_post = array(
'post_title'   => $user_info->username,
'post_status'  => 'publish', // <- here is to publish
'post_type'    => 'resume', // <- change to your cpt
'post_author'  => $user_id,
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}

我可以在用户注册后使用以下代码创建帖子:

add_action('user_register', 'resume_create', 10, 1);
function resume_create($user_id)
{
// Get user info
$user_meta = get_userdata($user_name);
// Create a new post
$user_post = [
'post_title' => $_POST['slug'],
'post_type' => 'resume',
'post_status' => 'publish',
'post_author' => $post_ID,
];
// Insert the post into the database
$post_id = wp_insert_post($user_post);
}

但是,不分配给当前用户和特殊角色。

相关内容

  • 没有找到相关文章

最新更新