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 already got code which can change role basis on post count.

<?php

  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' );

        get_currentuserinfo(); 
        global $user_level;

        // Do the checks to see if they have the roles and if not update them.
        if ( ($user_level != 10) && ( $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 ( ($user_level != 10) && ( $count > 5 && $count <= 9 ) && ( array_key_exists( 'author', $current_role[0] ) ) ) {

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

        } elseif ($user_level != 10){

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

        } return $post_id;

    }

?>

Now I have tested on local environent and it was working but now I dont know why it is giving me error as below.

Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in /home/....../themes/..../inc/functions/user-role.php on line 20

Warning: Cannot modify header information - headers already sent by (output started at /home/....../themes/..../inc/functions/user-role.php) in /home/...../wp-includes/functions.php on line 861

Please help me to solve this.

share|improve this question
add comment

1 Answer

You have to use just $current_role variable instead of $current_role[0], because call get_user_meta( $author->ID, 'wp_capabilities' ); will return you array with roles as keys.

<?php

  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' );

        get_currentuserinfo(); 
        global $user_level;

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

        } elseif ( ($user_level != 10) && ( $count > 5 && $count <= 9 ) && ( array_key_exists( 'author', $current_role ) ) ) {

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

        } elseif ($user_level != 10){

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

        } return $post_id;

    }

?>
share|improve this answer
    
I have tried error gone but now its not changing the role. Is there any other issue? –  pixelngrain Jun 5 '12 at 18:16
    
@pixelngrain i have updated the code, check it now. –  Eugene Manuilov Jun 5 '12 at 18:22
    
Thanks let me check.. meanwhile just quick question. Have edited any more things than remove [0]? just for knowledge –  pixelngrain Jun 5 '12 at 18:31
    
I have checked this code but its not changing the role. i just wonder whats wrong with it. :( –  pixelngrain Jun 5 '12 at 18:57
add 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.