Error while setting role

时间:2012-06-05 作者:pixelngrain

我已经得到了可以根据职位数量更改角色的代码。

<?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
请帮我解决这个问题。

1 个回复
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;

    }

?>

结束

相关推荐

编辑一个PHP文件以在每页上包含一小段文本

我正在建立一个网站(虚拟网站在www.teamcoding.ca/corporate上),希望在屏幕的右上角包含“切换到生活方式网站”,并链接到另一个网站。我的印象是,要做到这一点,我需要编辑我的一个。php文件。我试图编辑页面。php,全宽页面。php(我正在使用的模板),但没有看到任何结果。我还试着编辑标题。php,但没有看到任何结果。任何帮助都将不胜感激。