2017-06-13 165 views
1

如果用戶在管理頁面中擁有「Contributor」權限可以查看等待審覈的評論,但我需要禁用此選項。WordPress貢獻者審覈評論

在具有「查看評論等待審覈」的用戶角色中,它不存在任何內容。

如何爲Contributor用戶禁用/wp-admin/edit-comments.php?comment_status=moderated?

enter image description here

回答

1

修改意見查詢

這裏有一個建議,調整爲get_comments()輸入參數註釋狀態,與pre_get_comments行動的幫助:

add_action('pre_get_comments', function(\WP_Comment_Query $query) 
{ 
    // Only target edit-comments.php 
    if(! did_action('load-edit-comments.php')) 
     return; 

    // Only target users that can't publish posts 
    if(current_user_can('publish_posts')) 
     return; 

    // Halt the query for pending comments  
    if('hold' === $query->query_vars['status']) 
     $query->query_vars['status'] = 'non-existent'; 

    // Remove pending comments from the 'all' or '' status view 
    if(in_array($query->query_vars['status'], [ 'all', '' ], true)) 
     $query->query_vars['status'] = 'approve'; 
}); 

,我們的目標只有edit-comments.php頁並將狀態修改爲approve(如果爲空)或'all'(對於無法發佈帖子的用戶)。 在這裏,我們將評論狀態分配到一個不存在的狀態,以刪除掛起評論列表。

它可能有點混淆等待評論的所有各種狀態值,例如,

hold, pending, moderated, '0' 

取決於它是否是一個標籤,註釋查詢變量,或者它是如何存儲在數據庫中,

修改意見數

所有評論這裏算:

all

mean是Approved + Pending評論數的總和。

當我們更改評論查詢時,如上所述,這些評論狀態計數不會改變。我們可能也想調整一下。

下面是一個例子,我們如何可以通過wp_count_comments過濾器調整的評論數:

add_filter('wp_count_comments', 'wpse_count_comments', 10, 2); 

function wpse_count_comments($counts, $post_id ) 
{ 
    // Only target the backend 
    if(! is_admin()) 
     return $counts; 

    // Only target users that can't publish posts  
    if(current_user_can('publish_posts')) 
     return $counts; 

    // Avoid infinite loop before calling wp_count_comments()  
    remove_filter(current_filter(), __FUNCTION__);  
    $counts = wp_count_comments($counts, $post_id ); 
    add_filter(current_filter(), __FUNCTION__, 10, 2); 

    // Subract 'moderated' count from 'all' count 
    $counts->all = $counts->all - $counts->moderated; 

    // Set 'moderated' count to zero 
    $counts->moderated = 0; 

    return $counts; 
} 

這也將清除計數,不能發佈帖子的用戶,從這裏的管理菜單:

pending

修改註釋狀態鏈接

最後,我們可能要刪除未決註釋的狀態鏈接,對於不能發佈帖子的用戶:

add_filter('comment_status_links', function($status) 
{ 
    if(! current_user_can('publish_posts')) 
     unset($status['moderated']); 
    return $status; 
}); 

因此,這將成爲:

all

希望它能幫助!

+0

哇,完美!謝謝! – user3477026

1

你可以使用JavaScript添加腳本只用於記錄與貢獻者的角色與DOM未經批准的類刪除註釋行用戶繞過它。

檢查,如果用戶登錄並貢獻者:

if(is_user_logged_in() && current_user_can('contributor')) { 

然後添加腳本在線或排隊與follwing代碼js文件:

$('#the-comment-list tr.unapproved').remove(); 

檢查,如果該評論是在任何可見其他視圖並將相應的類添加到腳本中,以便將它們隨處移除

/*編輯*/

香草js腳本:

var elem = document.getElementById("the-comment-list"); 

for (var i = 0; i < elem.childNodes.length; i++) { 
    if (/\bunapproved/.test(elem.childNodes[i].className)) { 
     elem.childNodes[i].parentNode.removeChild(elem.childNodes[i]); 
    }   
} 
+0

此功能沒有配置貢獻者看到圖像,投稿人可以不適度的評論只看到等待審覈的評論,謝謝=( – user3477026

+0

function.php 功能frontfooter(){ 回聲「」 } 如果(is_user_logged_in()&& current_user_can。':$是不是一個功能 – user3477026

+0

我嘗試添加jQuery的管理,但不是一個好主意=( – user3477026

0

我發現這半的解決方案,從評論管理列表的代碼隱藏評論,但它刪除所有評論也主持評論,其確定現在=)

https://wordpress.stackexchange.com/questions/167250/prevent-contributor-to-show-comment-list

function filter_comments_by_contributor($all_comments) { 
    // get the current logged in user 
    $current_user = wp_get_current_user(); 

    if (0 == $current_user->ID) { 
     // Not logged in. 
     return $all_comments; 
    } else { 
     // Logged in. 

     // check if the logged-in user is a contributor 
     if (in_array('contributor', (array) $current_user->roles)) { 

      // check if the user is on wp-admin backend, 
      $screen = get_current_screen(); 
      if (! empty($screen) && 'edit-comments' == $screen->id) { 

       // get all posts by that contributor 
       $args    = array(
        'author'   => $current_user->ID, 
        'posts_per_page' => - 1, 
        'fields'   => 'ids' 
       ); 
       $contributor_posts = get_posts($args); 

       // unset the comments given on posts other than his/her. 
       foreach ($all_comments as $key => $value) { 
        if (! in_array($value->comment_post_ID, $contributor_posts)) { 
         unset($all_comments[ $key ]); 
        } 
       } 
      } 

      return $all_comments; 
     } else { 
      return $all_comments; 
     } 
    } 
} 

if(is_user_logged_in() && !current_user_can('manage_options')) { 
    add_filter('the_comments', 'filter_comment_by_contributor'); 
} 
0

我建議你使用user role editor會對一般用戶的角色完整的控制和一些例外添加到特定的用戶。