2017-10-01 3 views
1

これは私がやろうとしていることです。私はプラグインに取り組んでおり、それぞれのコメントをチェックして、そのコメントの作成者が「管理者」または「エディタ」の役割を持っているかどうかを確認したい。彼らがユーザー名とアバターを表示するのではなく、会社のロゴなどと一緒にウェブサイトの名前を表示したいと考えています。私は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()を使用することができます。filterを使用して

$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

お世話になりました、ありがとうございました。私は役割をチェックしなければなりません、ユーザ名は変更され、他は追加されます。私はチェックする必要がある2つの異なる役割を持っています: "エディタ"、 "管理者"。また、私はどこに引っ掛けるだろうか?私はフィルターを使いますか? – jared10222

0

が進むべき道でした。私は自分のニーズに合った2つのフックを見つけました: 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); 
関連する問題