Wordpress:根据职位数有条件地更改角色 [英] Wordpress: Conditionally role change basis on post count

查看:260
本文介绍了Wordpress:根据职位数有条件地更改角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since almost 4-5 months I am trying to solve this issue where user role automatic change with certain number of post count. Say if x user have <= 10 post he/she is in contributor role if <=20 author role so on..

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

With help of you all people I could achieve this code.

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

    }

?>

这段代码工作了一段时间,根本没有反应。这段代码有错误,如果一旦以某种方式可以自动更改角色,并且如果我再次将角色更改为向下,则该代码将永远不会根据帖子数自动更改。同样,如果我删除帖子,则完全不受影响。但是,它应该始终考虑发布数量,并立即影响发布数量。

This code working some time and some time doesnt reacting at all. This code is buggy and if once somehow will auto change the role and if I change role again back to down side than it will never auto change as per post count. same if I delete post than its not affecting at all. However it should always consider the post count and imminently should effect with the number of post.

我对此实在感到厌倦,但是我的整个网站仅基于此代码。因此,我真的需要您的大力帮助。

I am really getting tired with this but my entire site is based on this code only. So I really need you great people help.

非常感谢。.

推荐答案

我认为在启动用户角色时存在错误,因为:

what i think is there is a mistake in starting users role because:

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,则设置用户角色

If user level is 10 AND post count is major than 3 AND minor or equal than 5 set user role to 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' );

您再说一次,条件标签仅在用户角色为10时才启动...例如,如果用户发布了4条帖子,并且其角色是作者,那么该条件标记将无效

You say again that conditional tag starts only IF THE USER ROLE IS 10... so, for example, if users have posted 4 posts, and it's role is author the conditional tag will not works

这篇关于Wordpress:根据职位数有条件地更改角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆