在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',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);
}

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