admin管理员组

文章数量:1295948

wonder if you can help me.

I'm trying to get an action (geodir_after_save_listing) to change a user role.

Basically, in Geo Directory, when the user submits a business listing, I want to change their role from "member" to "member_with_business"

I believe the geo_dir_after_save_listing is still a relevant action (awaiting response from GeoDirectory support) so IMO the php snippet below should work?

add_action('geodir_after_save_listing', 'change_user_role_when_submit_business');

function change_user_role_when_submit_business( $last_post_id, $request_info ) {
  $gd_post_type = geodir_get_current_posttype();

    if ($gd_post_type == 'gd_place') {

    $user = wp_get_current_user();
    $roles = $user->roles;
    
    if (in_array('member', $roles)){
      
        $user->add_role( 'member_with_business' );
        $user->remove_role( 'member' );
    }
  }
}

Any thoughts from those more php savvy than me?

wonder if you can help me.

I'm trying to get an action (geodir_after_save_listing) to change a user role.

Basically, in Geo Directory, when the user submits a business listing, I want to change their role from "member" to "member_with_business"

I believe the geo_dir_after_save_listing is still a relevant action (awaiting response from GeoDirectory support) so IMO the php snippet below should work?

add_action('geodir_after_save_listing', 'change_user_role_when_submit_business');

function change_user_role_when_submit_business( $last_post_id, $request_info ) {
  $gd_post_type = geodir_get_current_posttype();

    if ($gd_post_type == 'gd_place') {

    $user = wp_get_current_user();
    $roles = $user->roles;
    
    if (in_array('member', $roles)){
      
        $user->add_role( 'member_with_business' );
        $user->remove_role( 'member' );
    }
  }
}

Any thoughts from those more php savvy than me?

Share Improve this question asked Apr 8, 2021 at 20:41 Mark LeeMark Lee 1
Add a comment  | 

1 Answer 1

Reset to default 0

Couple of things perhaps spring to mind - when using add_action you'll need to provide how many of the arguments you're planning on using. Default 1. You're not using request info so get rid of that. Second bit geodir_get_current_posttype() link

Read through how the function works and make sure it makes sense and would work in the context you are using it. It looks like it reads a query var from the URL - is that there?

Another option could just be to look this up yourself - looks like you've got the $last_post_id variable. Can you not find out the info you need from that?

Plenty of var_dumps and die can help you diagnose what you're missing. Looks like the plugin author leaves most of them in the code anyway!

本文标签: On actionchange user role