我已经得到了可以根据职位数量更改角色的代码。
<?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;
    }
?>
 现在我已经测试了当地的环境,它是工作,但现在我不知道为什么它给我以下的错误。
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
 请帮我解决这个问题。
 
                SO网友:Eugene Manuilov
                你必须使用$current_role 变量而不是$current_role[0], 因为电话get_user_meta( $author->ID, \'wp_capabilities\' ); 将返回以角色作为键的数组。
<?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;
    }
?>