0

自从将近 4-5 个月以来,我一直在尝试解决这个问题,即用户角色会随着一定数量的帖子计数而自动更改。假设 x 用户有 <= 10 个帖子,如果 <=20 作者角色,则他/她处于贡献者角色,依此类推..

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

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

    }

?>

这段代码工作了一段时间,有时根本没有反应。这段代码有问题,如果曾经以某种方式自动更改角色,并且如果我再次将角色更改回向下,那么它将永远不会根据帖子计数自动更改。如果我删除帖子而不是它根本不影响,则相同。但是,它应该始终考虑帖子数量,并且立即应该影响帖子数量。

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

非常感谢。。

4

1 回答 1

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 个帖子,并且它的角色是作者,则条件标签将不起作用

于 2012-07-17T07:08:57.413 回答