0

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.

1 Answer 1

2

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;

    }

?>
4
  • I have tried error gone but now its not changing the role. Is there any other issue? Jun 5, 2012 at 18:16
  • @pixelngrain i have updated the code, check it now. Jun 5, 2012 at 18:22
  • Thanks let me check.. meanwhile just quick question. Have edited any more things than remove [0]? just for knowledge Jun 5, 2012 at 18:31
  • I have checked this code but its not changing the role. i just wonder whats wrong with it. :( Jun 5, 2012 at 18:57

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.