【问题标题】:Wordpress: Conditionally role change basis on post countWordpress:根据帖子计数有条件地改变角色
【发布时间】:2012-06-10 20:40:04
【问题描述】:

自从将近 4-5 个月以来,我一直在尝试解决这个问题,即用户角色会随着一定数量的帖子计数而自动更改。假设 x 用户有

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

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

    }

?>

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

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

非常感谢..

【问题讨论】:

    标签: wordpress post conditional role


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 2012-02-29
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多