让管理员和帖子作者都更新评论元

时间:2014-03-17 作者:dkl

我有一个功能,可以让帖子作者设置“最佳评论”。我希望管理员也能够设置“最佳评论”。

谁能最好地实现这一点?

// Set Best comment for post
function select_best_comment($best_comment_id, $best_comment_parent_question) {
    // Prevent Non Logged In Users from making updates
    if ( !is_user_logged_in() ) { return new WP_Error(\'not_logged_in_post\', \'sorry cant do this\'); }

    $post_data = get_post($best_comment_parent_question);
    $current_user = wp_get_current_user();

// Make sure the post author is selecting the best comment
        if ($current_user->ID == $post_data->post_author) {

            include (TEMPLATEPATH . \'/translations.php\');   

            global $wpdb;
            $get_comments = "SELECT * FROM " . $wpdb->prefix . "comments WHERE comment_post_ID =\'". $best_comment_parent_post . "\'" ;
            $best_post_comments = $wpdb->get_results($get_comments);

            $best_comment = get_comment($best_comment_id);

            foreach($best_post_comments as $comment) :
                setup_postdata($comment);
                update_comment_meta($comment->comment_ID, \'best_comment\', \'no\');
            endforeach;

            $currentPointNumber = intval(get_user_meta($best_comment->user_id, \'points\', true));

            $newPointNumber = $currentPointNumber + 2;  

            $best_comment_user = get_userdata($best_comment->user_id);

            update_comment_meta($best_comment_id, \'best_comment\', \'yes\');

            // Add 2 Points for being Selected as Best comment
            $has_been_best_comment = get_comment_meta($best_comment_id, \'has_been_best_comment_before\', true);
            if ($has_been_best_comment == \'no\' || $has_been_best_comment == \'\') {
                update_user_meta($best_comment->user_id, \'points\', $newPointNumber);
                update_comment_meta($best_comment_id, \'has_been_best_comment_before\', \'yes\');
            }

            setcookie("best_comment_selected_".$best_comment_parent_post,"yes", time()+10);
            $post_data = get_post($best_comment_parent_post);
            wp_redirect($post_data->guid);  
        }
    }

1 个回复
最合适的回答,由SO网友:birgire 整理而成

您可以尝试替换

if ($current_user->ID == $post_data->post_author) {
使用

if ( $current_user->ID == $post_data->post_author 
    || $current_user->has_cap( \'manage_options\' ) ) {
要另外允许用户,请使用正确的capability, 投票选出最佳评论。

结束