assign user role on submitting listing

This topic contains 6 replies, has 2 voices, and was last updated by  entoen 4 years, 11 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #525306

    entoen
    Free User
    Post count: 6

    hi, was using a snippet to change the user role on user submitting a listing. It worked beautiful but not with GeoDit CPT. What am i doing wrong?

    /*change user role when MORE then xxx posts on login*/
    function custom_update_roles( $user_login, $user ) {
        if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
            if ( in_array( "subscriber", $user->roles ) ) 
            {
            $user_id =$user->ID;
            $post_count=count_user_posts( $user_id,'gd_place' );
                if ( $post_count > 0 ) {
                  // Success.
                    $u = new WP_User( $user_id );
                    $u->remove_role( 'subscriber' );
                    $u->add_role( 'author' );
                } 
            } 
    
            else
            {
            // This user is not a subscriber.
            }
        }
    }
    add_action( 'wp_login', 'custom_update_roles', 10, 2 );
    #525331

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Hi entoen,

    You are only checking for gd_place post type, try checking for all
    geodir_get_posttypes()

    /*change user role when MORE then xxx posts on login*/
    function custom_update_roles( $user_login, $user ) {
        if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
            if ( in_array( "subscriber", $user->roles ) ) 
            {
            $user_id =$user->ID;
            $gd_post_types = geodir_get_posttypes();
            $post_count=count_user_posts( $user_id, $gd_post_types );
                if ( $post_count > 0 ) {
                  // Success.
                    $u = new WP_User( $user_id );
                    $u->remove_role( 'subscriber' );
                    $u->add_role( 'author' );
                } 
            } 
    
            else
            {
            // This user is not a subscriber.
            }
        }
    }
    add_action( 'wp_login', 'custom_update_roles', 10, 2 );

    Thanks,

    Stiofan

    #525850

    entoen
    Free User
    Post count: 6

    hi Stiofan, thank you for your answer. It isn’t working. Why would checking for post types work? I am only using gd_place as a post-type.

    Han

    #528201

    entoen
    Free User
    Post count: 6

    Any idea’s anybody?

    #528224

    Stiofan O’Connor
    Site Admin
    Post count: 22956

    Sorry i thought u meant to use it with CPT addon.
    Your original code looks ok to me, maybe its the placement.

    Stiofan

    #528295

    entoen
    Free User
    Post count: 6

    I thought so too, and it is working!

    one last adjustment i cannot seem to implement is that i only want the published post to be counted. Is that possible?

    I tried adding “->publish” but it doesn’t do the job:

    function custom_update_roles( $user_login, $user ) {
        if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
            if ( in_array( "author", $user->roles ) ) 
            {
            $user_id =$user->ID;
            $post_count=count_user_posts( $user_id,'gd_place' )->publish;
                if ( $post_count = 0 ) {
                  // Success.
                    $u = new WP_User( $user_id );
                    $u->remove_role( 'author' );
                    $u->add_role( 'subscriber' );
                } 
            } 
    
            else
            {
            // This user is not a subscriber.
            }
        }
    }
    add_action( 'wp_login', 'custom_update_roles', 10, 2 );
    #528296

    entoen
    Free User
    Post count: 6

    got it!
    cannot use $post_count = 0

    so i turned to the expired listings; this works fine:

    /*change user role when MORE then xxx posts on login*/
    function custom_update_roles( $user_login, $user ) {
        if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
            if ( in_array( "author", $user->roles ) ) 
            {
            $user_id =$user->ID;
            $post_count=count_user_posts( $user_id,'gd_place' )->expire;
                if ( $post_count = 1 ) {
                  // Success.
                    $u = new WP_User( $user_id );
                    $u->remove_role( 'author' );
                    $u->add_role( 'subscriber' );
                } 
            } 
    
            else
            {
            // This user is not a subscriber.
            }
        }
    }
    add_action( 'wp_login', 'custom_update_roles', 10, 2 );
Viewing 7 posts - 1 through 7 (of 7 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount