0

I've been trying to figure out the solution for the following scenario. I've got a custom post type - job_listing using the WP Job Manager plugin.

What I am trying to achieve is to change the default user role depending on the job_listing post count. So by default, the user's role is 'subscriber', but whenever the user submits a job listing, this user role should automatically change to 'employer'.

I put together a piece of code collected from various tuts based on this. But i still can't make this thing working.

Here is my code in the functions.php:

add_action ('publish_post', 'update_roles');
function update_roles ($post_type = 'job_listing', $post_status = 'publish') {

   global $wpdb;
   $author = wp_get_current_user();

   $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $author->ID AND post_type = '$post_type' AND post_status = '$post_status'"; 
    $count = $wpdb->get_var($query);

   if($count > 0 && current_user_can('subscriber'))
   {
       // Remove role
       $author->remove_role( 'subscriber' );

       // Add role
       $author->add_role( 'employer' );

   }

} 

What is it that i'm doing wrong there?

Joe Bloggs
  • 1,410
  • 2
  • 24
  • 53

1 Answers1

0

Not tested but I was watching this question from this morning and noticed no one was answering, so I thought to answer it . please try this .

add_action( 'save_post', 'change_author_role', 3 );
function change_author_role( $post_id ){
    $slug = 'job_listing';
    global $post;
    // If this isn't a 'job_listing' post, don't update it.
    if ( $slug != $post->post_type ) {
        return;
    }

    //here check if already a employer or admin 

    wp_update_user( array('ID' => get_current_user_id(), array( 'roles' => array( 'Subscriber', 'Administrator' ) ) ) );

    //$role = array( 'Subscriber', 'Administrator' );
    //$user = new WP_User(get_current_user_id());
    //$user->set_role($role);

}
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • Hi there, thanks so much for the effort, tried your solution, but nope, still doesn't seem to do anything .. – Joe Bloggs Sep 17 '16 at 21:55
  • @JoeBloggs I just updated the code to provide the id of author to `wp_update_user` try this or we need to change the action hook . – Prafulla Kumar Sahu Sep 18 '16 at 15:46
  • thanks so much again. I'm getting a syntax error in this line of your code: `wp_update_user( 'ID' => $_POST['listing_author'], array( 'roles' => array( 'employer' ) ) );` and just can't figure out what could be wrong there. Can you please double check? – Joe Bloggs Sep 20 '16 at 09:24
  • @JoeBloggs I will look into it when I will be at home may be after 3/4 hours for now try to go through parameters wp_update_user takes and try to make it correct or let me know what exactly syntax error you are getting . and you can also try ```update_user_meta( $this->ID, $this->cap_key, $this->caps );``` if you are unable to debug this, I look into it when I will be at home. – Prafulla Kumar Sahu Sep 20 '16 at 14:13
  • Thanks a lot, I really appreciate your help @PrafullaKumarSahu. I'm using dreamweaver to edit the php file, when I input your code, i'm getting error in that line mentioned earlier, meaning it could just be a wrong or missing character somewhere, like `,` or `;` or and extra or missing `)` something similar, really minor, but I can't figure what it could be. I was coming out of similar post here [link]http://stackoverflow.com/questions/10920862/wordpress-conditionally-role-change-basis-on-post-count , if it's any help. – Joe Bloggs Sep 20 '16 at 21:13
  • And one more similar post here [link]http://stackoverflow.com/questions/9181440/wordpress-change-user-role-conditionally – Joe Bloggs Sep 20 '16 at 21:15
  • @JoeBloggs It is really great that you are still trying, I am little ill that is why I did not worked last night after office, but your effort is forcing me to do it my self and let you know, I will try to do it myself as I have already a setup wordpress site with this plugin and i will let you know what ever progress I can make . – Prafulla Kumar Sahu Sep 21 '16 at 06:10
  • I really appreciate it, thanks so much for your help on this and I hope you'll get better soon .. – Joe Bloggs Sep 21 '16 at 08:33
  • @JoeBloggs now the current code should work for you , if it does not work comment the wp_update_user() and comment out the commented lines after this function and it will do the job. – Prafulla Kumar Sahu Sep 21 '16 at 20:21
  • ** you need to modify the roles, i have used ```'Subscriber', 'Administrator' ``` only for test. – Prafulla Kumar Sahu Sep 21 '16 at 20:22
  • @JoeBloggs do not try it for administrator , I am still testing for exceptions , try it for normal user . – Prafulla Kumar Sahu Sep 21 '16 at 20:32
  • all tested, but still can't make it working. I tried the first combination and then commented out the `wp_update_user` line and tried the second combination as per your suggestion. I replaced to roles in your code to test it with "employer" role. Basically, all my users are registered as "subscribers" by default. But once they submit a job_listing, I need their role to be automatically updated to "employer" role. (but check first if they are already "employer" and if they are already "employer" then do nothing). – Joe Bloggs Sep 22 '16 at 21:16
  • @JoeBloggs visit wp_usermeta table and check how it is stored that may give you some idea, today I am leaving for a journey of two days, i will come back on Sunday so, you need try your own today . – Prafulla Kumar Sahu Sep 23 '16 at 03:54