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;
}
?>