3pepe3
3pepe3

Reputation: 613

Wordpress assign page per user

When a new user register to the site, is there any way to create and assign a page (custom post type) to this new user ?

Ex.

--Business owner (user profile)
    |___ Business page (custom post type business)

Any idea ?.

thanks Pepe

Upvotes: 0

Views: 159

Answers (1)

3pepe3
3pepe3

Reputation: 613

ok... actually it wasn't very hard.

Reading the hook's reference from wordpress I write this :

function add_business_page($user_id){
    $user = get_userdata($user_id);
    if( reset($user->roles) != "adherent" ) return;
    $post_type = 'commerce';
    global $wpdb;
    $where = get_posts_by_author_sql( $post_type, true, $user_id );
    if( $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ) >= 1 ) return; 
        $user_page = array(
            'post_title' => "Titre de la page : " . $user->user_login,
            'post_type' => 'commerce',
            'post_content' => '',
            'post_author' => $user_id,
            'post_status' => 'publish'
            );

    wp_insert_post( $user_page );
}
add_action('user_register', 'add_business_page');
add_action('set_user_role', 'add_business_page');

So you can create a user or modify the role and just one page per user is going to be created and assigned to that user.

Upvotes: 1

Related Questions