Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to update a users role based on number of posts published when the user publishes a new post. I have tried this code but it does not work.

    add_action('publish_post', 'update_roles');

    function update_roles()
    {

       global $wpdb;

       // Get the author
       $author = wp_get_current_user();

        $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

        $numPost = count($posts);

        // Do the checks to see if they have the roles and if not update them.
        if($numPost > 0 && $numPosts <= 2 && current_user_can('subscriber'))
        {

            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role('contributor'); 

        } elseif ($numPost > 3 && $numPosts <= 5 && current_user_can('contributor'))
        {
            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role('author'); 

        } elseif ($numPost > 6 && $numPosts <= 9 && current_user_can('author'))
        {

            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role('author'); 

        }


    }

?>

I have also tried this and it is not working either.

<?php

    add_action('publish_post', 'update_roles');

    function update_roles()
    {

       global $wpdb;

       // Get the author
       $author = wp_get_current_user();


       // Get post by author
       $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

       $numPost = count($posts);

       // Do the checks to see if they have the roles and if not update them.
       if($numPost > 0 && $numPosts <= 2 && current_user_can('subscriber'))
       {
           // Remove role
           $author->remove_role( 'subscriber' );

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

       } elseif ($numPost > 3 && $numPosts <= 5 && current_user_can('contributor'))
       {
           // Remove role
           $author->remove_role( 'contributor' );

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

       } elseif ($numPost > 6 && $numPosts <= 9 && current_user_can('author'))
       {
           // Remove role
           $author->remove_role( 'author' );

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

       }


    }

?>

EDIT NEW: Can you please tell me either I have done correctly or not?

<?php

    add_action('publish_post', 'update_roles');

    function update_roles()
    {

       global $wpdb;

       // Get the author
       $author = wp_get_current_user();

       // Get post by author
       $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_author = " . $author->ID );

       $numPost = count($posts);

       // Do the checks to see if they have the roles and if not update them.
       if ($numPost > 3 && $numPosts <= 5 && array_key_exists( 'contributor', $old_role ) )
        {
            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role('author'); 

        } elseif ($numPost > 6 && $numPosts <= 9 && array_key_exists( 'author', $old_role ) )
        {

            $user_id_role = new WP_User($user_id);
            $user_id_role->set_role('editor'); 

        }


    }

?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

current_user_can() checks for a capability, eg: edit_posts, not a role.

The only capabilities subscribers have is read which only gives them access to the dashboard to change their profile (unless you added additional caps). They can't even publish a post so you will have to start with contributors.

$old_role = get_user_meta( $user_id, 'wp_capabilities' );
elseif ($numPost > 3 && $numPosts <= 5 && array_key_exists( 'contributor', $old_role ) )
    {
        $user_id_role = new WP_User($user_id);
        $user_id_role->set_role('author'); 

    } elseif ($numPost > 6 && $numPosts <= 9 && array_key_exists( 'author', $old_role ) )
    {

        $user_id_role = new WP_User($user_id);
        $user_id_role->set_role('author'); 

    }

What we are doing is using get_user_meta() to retrieve the value of wp_capabilities from the usermeta table. Since the value of that field is an array: Array ( [contributor] => 1 )

and the role is one of the keys we can use the php array_key_exists() function to check if the role exists for that user. Also adding to this that instead of running your custom SQL you can use the count_user_posts() function to get the post count.

Full Example:

Update: This is fully tested and works.

    add_action( 'save_post', 'update_roles' );
    function update_roles( $post_id ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        // Get the author
        $author = wp_get_current_user();

        // Set variables for current user information
        $count = count_user_posts( $author->ID );
        $current_role = (array) get_user_meta( $author->ID, 'wp_capabilities' );

        // Do the checks to see if they have the roles and if not update them.
        if ( ( $count > 3 && $count <= 5 ) && ( array_key_exists( 'contributor', $current_role[0] ) ) ) {
            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( 'author' );

        } elseif ( ( $count > 6 && $count <= 9 ) && ( array_key_exists( 'author', $current_role[0] ) ) ) {

            $user_id_role = new WP_User( $author->ID );
            $user_id_role->set_role( 'editor' );

        } return $post_id;

    }
share|improve this answer
    
I have tried but no result must be doing something wrong. Can you please check the third code after EDIT NEW: either I have done correctly or not. Please –  pixelngrain May 27 '12 at 16:07
    
Your code is missing get_user_meta() to assign the array to the $old_role variable. –  Chris_O May 27 '12 at 16:11
    
Can you please explain me little in detail? I am not expert and need some detail guidance. Thanks –  pixelngrain May 27 '12 at 16:15
    
I updated the answer with fully working and tested code. I changed the action to save_post because contributors can't publish. –  Chris_O May 27 '12 at 17:15
    
Wow! your are amazing. Fuf I was finding solution since so long and you made it. Thank you so much. Just one question. Will it work with existing user or only with new user after implementation of this code? Also if I will create my own role will it work with this? –  pixelngrain May 27 '12 at 17:57
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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