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 TicketTagged: change user role add listing
-
AuthorPosts
-
January 13, 2020 at 7:35 am #525306
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 );
January 13, 2020 at 12:06 pm #525331Hi 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
January 15, 2020 at 9:30 pm #525850hi 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
January 31, 2020 at 10:43 am #528201Any idea’s anybody?
January 31, 2020 at 1:26 pm #528224Sorry i thought u meant to use it with CPT addon.
Your original code looks ok to me, maybe its the placement.Stiofan
January 31, 2020 at 9:16 pm #528295I 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 );
January 31, 2020 at 9:21 pm #528296got it!
cannot use $post_count = 0so 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 );
-
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket