2017-10-01 1 views
1

그래서 여기에 내가하려고하는 것이있다. 나는 플러그인을 작성 중이고 각 주석을 확인하고 해당 주석의 작성자가 "관리자"또는 "편집자"역할을하는지 확인하려고합니다. 사용자 이름과 아바타를 표시하는 대신 웹 사이트의 이름과 회사 로고 등을 표시하고 싶습니다. 나는 WordPress 개발에 익숙하지 않아이 문제에 집착하고있다. 이 필터가 있는지 또는 사용자 지정 메모 서식 파일을 만들어야하는지 알 수 없습니다. 누군가가 나를 올바른 방향으로 향하게 할 수 있다면 그것은 좋을 것입니다. 왜냐하면이 시점에서 나는 어디에서 시작해야할지 모르겠습니다. 고맙습니다.특정 역할과 일치하는 경우 사용자 이름을 주석의 사이트 이름으로 표시하십시오. 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'); 

답변

0

것은, 주석 이름은 "관리자"있는지 확인,이 같은 :

// 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 
     } 
    } 
+0

안녕하세요 Dolatabadi, 응답 해 주셔서 감사합니다. 역할을 확인해야하며 사용자 이름이 변경되고 다른 사용자 이름이 추가됩니다. 내가 확인해야 할 2 가지 역할이 있습니다 : "편집자", "관리자". 또한, 나는 어디에서 훅을 걸 것이냐? 필터를 사용합니까? – jared10222

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); 
관련 문제