0

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?

1 Answer 1

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!

1
  • Thanks @Bysander, apologies I didn't see your response till now. I had some support from the plugin developer and this code seemed to work. add_action( 'geodir_post_saved', 'gd_snippet_210409_on_post_saved', 9999, 4 ); function gd_snippet_210409_on_post_saved( $postarr, $gd_post, $post, $update ) { if ( $gd_post['post_type'] == 'gd_place' ) { $user = wp_get_current_user(); if ( ! empty( $user->roles ) && in_array( 'member', $user->roles ) ) { $user->set_role( 'member_with_business' ); } } } However on page load, it instantly saves and changes member role
    – Mark Lee
    Jun 22, 2021 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.