0

I have a function which lets the post author set the "best comment". I want the admins to also be able to set the "best comment".

Who can this be best achieved?

// 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);  
        }
    }
  • I solved it with Mr. Birgire's solution. – dkl Mar 25 '14 at 7:18
2

You can try to replace

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

with

if ( $current_user->ID == $post_data->post_author 
    || $current_user->has_cap( 'manage_options' ) ) {

to additionally allow users, with the correct capability, to vote for the best comment.

  • Happy to hear it worked for you, cheers – birgire Mar 25 '14 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.