【问题标题】:Make user's name display as the site name in comments if it matches certain roles | wordpress如果匹配某些角色,则将用户名显示为评论中的站点名称 | WordPress
【发布时间】:2017-10-02 10:13:32
【问题描述】:

这就是我想要做的。我正在开发一个插件,我想检查每条评论,看看该评论的作者是否具有“管理员”或“编辑”的角色。如果他们这样做,而不是显示他们的用户名和头像,我想显示网站的名称以及公司徽标或其他东西。我对 WordPress 开发还很陌生,并且一直坚持这一点。我不知道是否有一个过滤器,或者我是否需要创建一个自定义 cmets 模板。如果有人甚至可以让我指出正确的方向,那就太好了,因为在这一点上,我什至不确定我应该从哪里开始。谢谢。

我在哪里,我的思考过程:

<?php 
function anonymize_author(){
    global $post;

    //get the id of the comment author
    $author_id = $post->post_author;

    //get the userdata of comment author
    $author_info = get_userdata($author_id);

    //get the user roles of comment author
    $author_roles = $author_info->roles;

    //Array of roles to check against
    $roles_to_check = ["editor", "administrator"];

    //see if user has a role in my $roles_to_check array
    $results = array_intersect($roles_to_check, $author_roles);

    if(!empty($results)){
        //the user has roles of either "editor" or "administrator"
        //load custom comments page?
        //I need to display the author name as the site name
        //and the avatar as the site logo
    }else{
        //Just a regular user, load the Wordpress Default comments
    }
}

add_filter('some_filter_here', 'anonymize_author');

【问题讨论】:

    标签: php wordpress comments


    【解决方案1】:

    显示 cmets 时,查看评论名称是否为“admin”,如下所示:

    // check to see if the comment author name is admin
    if ( "admin" == get_comment_author() ) {
       // your condition
    }
    

    为此,在以站点管理员身​​份发表评论时,您应该使用名称admin。您也可以使用get_comment_author_email() 匹配管理员邮箱:

        $all_users = get_users(); // get all users
        // loop through all users
        foreach ($all_users as $user) {
            // if the user email matches the commenter's email
            // and user has admin role
            if ( $user->user_email == get_comment_author_email() && in_array( 'administrator', (array) $user->roles ))
            {
                // your condition
            }
        }
    

    【讨论】:

    • 您好 Dolatabadi,感谢您的回复。我必须检查角色,用户名会改变,其他人会被添加。我需要检查 2 个不同的角色:“编辑”、“管理员”。还有,我要挂在哪里?我会使用过滤器吗?
    【解决方案2】:

    使用filter 是可行的方法。我找到了两个适合我需要的钩子: get_comment_author_linkget_avatar_url。 以下是我如何将它们应用于我的问题:

    //plugin.php
    
    /*
     * A function that compares users roles
     */
    function check_user_roles($user_roles){
        //array of roles to check
        $roles_to_check = ["editor", "administrator"];
    
        //see if user roles match any $roles_to_check values
        $results = array_intersect($roles_to_check, (array) $user_roles);
    
        if(!empty($results)){
            return true;
        }else{
            return false;
        }           
    }
    
    /*
     * Callback function for the get_comment_author_link filter
     */
    function anonymize_author($return, $author, $comment_id){
        //find comment data
        $comment_data = get_comment($comment_id);
    
        //find authors user_id
        $user_id = $comment_data->user_id;
    
        //check that author is a registered user before proceeding
        if($user_id > 0){
            //get user data
            $user = get_user_by('id', $user_id);
    
            //get users roles
            $user_roles = $user->roles;
    
            if(check_user_roles($user_roles)){
                $author = bloginfo('name');
            }
        }
        return $author;
    }
    
    /*
     * Callback function for the get_avatar_url filter
     */
    function anonymizer_avatar($url, $id_or_email, $args){
        $user = false;
        if(is_numeric($id_or_email)){
            $id = (int) $id_or_email;
            $user = get_user_by('id', $id);
        }elseif(is_object($id_or_email)){
            if(!empty($id_or_email->user_id)){
                $id = (int) $id_or_email->user_id;
                $user = get_user_by('id', $id);
            }
        }else{
            $user = get_user_by('email', $id_or_email);
        }
    
        if($user && is_object($user)){
            $user_roles = $user->roles;
            if(check_user_roles($user_roles)){
                $url = "URL_TO_MY_IMAGE";
            }
        }
        return $url;
    }
    
    add_filter('get_avatar_url', 'anonymize_avatar', 1, 3);
    add_filter('get_comment_author_link', 'anonymize_author', 1, 3);
    

    【讨论】:

      猜你喜欢
      • 2017-04-08
      • 2014-09-23
      • 2016-03-06
      • 1970-01-01
      • 2021-04-06
      • 2016-04-21
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      相关资源
      最近更新 更多