如果用户的名称与某些角色匹配,则将其显示为注释中的站点名称WordPress的

时间:2017-10-01 00:11:23

标签: php wordpress comments

所以这就是我想要做的。我正在制作一个插件,我想查看每个评论,看看该评论的作者是否具有“管理员”或“编辑”的角色。如果他们这样做,而不是显示他们的用户名和头像,我想显示网站的名称以及公司徽标或其他东西。我是WordPress开发的新手,我坚持这个。我无法弄清楚是否有针对此的过滤器,或者我是否需要创建自定义注释模板。如果有人甚至可以让我指向正确的方向,这将是伟大的,因为在这一点上,我甚至不知道我应该从哪里开始。谢谢。

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

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

2 个答案:

答案 0 :(得分:0)

显示评论时,请检查评论名称是否为“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
        }
    }

答案 1 :(得分:0)

使用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);