admin管理员组

文章数量:1335854

Can anyone help me to create a custom post type content when a new user is registered? But the user should be of specific role only.

I have been searching but couldn't find any solution.

Can anyone help me to create a custom post type content when a new user is registered? But the user should be of specific role only.

I have been searching but couldn't find any solution.

Share Improve this question edited May 27, 2020 at 9:30 fuxia 107k38 gold badges255 silver badges459 bronze badges asked May 27, 2020 at 8:20 user3626802user3626802 1
Add a comment  | 

1 Answer 1

Reset to default 0

Try below code:

add_action( 'user_register', 'create_new_post_onuser_registration', 10, 1 );
function create_new_post_onuser_registration( $user_id ){
    // Get user info
    $user_meta = get_userdata( $user_id );

    $user_roles = $user_meta->roles;

    // Update the role here
    if ( in_array( 'subscriber', $user_roles ) ) {
        // Do something.

        // Create a new post
        $subscriber_post = array(
            'post_title'   => $user_meta->nickname;
            'post_content' => $user_meta->description,
            'post_type'    => 'custom_post_type', // Update to your custom post type
        );

        $post_id = wp_insert_post( $subscriber_post );

        // Add custom data
        add_post_meta( $post_id, 'user_email', $user_meta->user_email );
    }    
}

本文标签: Creating a custom post type upon registration for a specific user role