Wordpress:有条件的角色更改基础上的帖子计数

时间:2012-06-06 19:29:02

标签: wordpress post conditional role

由于差不多4-5个月我试图解决这个问题,其中用户角色自动更改一定数量的帖子计数。假设如果&x = 20作者角色,x用户在< = 10后发布他/她是贡献者角色...

在所有人的帮助下,我可以实现此代码。

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

    }

?>

这段代码工作了一段时间,有些时间根本没有反应。这个代码是错误的,如果一旦某种方式将自动更改角色,如果我再次将角色更改回到下方,它将永远不会根据帖子计数自动更改。同样,如果我删除帖子比完全不影响。但是,它应该始终考虑帖子计数,并立即应该对帖子的数量产生影响。

我对此感到厌倦,但我的整个网站仅基于此代码。所以我真的需要你们的帮助。

非常感谢..

1 个答案:

答案 0 :(得分:0)

我认为启动用户角色有误,因为:

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

如果用户级别为10且后期计数大于3且次要或等于5将用户角色设置为作者

但在第二个

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

你再次说条件标签仅在用户角色为10时启动...例如,如果用户发布了4个帖子,并且它的作用是作者,条件标签将无效