Like and Dislike Buttons on Post with Counter – Only allow one click per post per user session

Question

Working on a "Like" vs "Dislike" for blog post in WordPress. While all is working fine, the intention here is to make it work for guests and logged in users, but more importantly; to only allow one click (Like or Dislike) per post from the same user (trying to understand if I can use WordPress sessions here?).

Idea:

Guest / registered and logged in user clicks on Like or Dislike = both button for that specific post are deactivated (cannot be clicked again). If the same user visits a different post on the same website where the user has not yet clicked any of the two buttons; they can click either Like or Dislike.

I hope I am being clear and that you all understand me. I’m looking for advice and code examples and what not as to how to achieve this.

As of now; a user (logged in or not) can click on Like or Dislike as many times as they want and the count just keeps on ticking — which is NOT ideal.

This is the code I’m working on:

add_filter( 'the_content', 'post_likes' );
function post_likes( $content ) {

    // only on posts
    if ( is_singular( 'post' ) ) {
        
        ob_start();

        ?>
            <ul class="likes">

                <li class="likes__item likes__item--like">
                    <a href="<?php echo add_query_arg( 'post_action', 'like'); ?>">
                        Useful (<?php echo get_post_like_count('likes') ?>)
                    </a>
                </li>

                <li class="likes__item likes__item--dislike">
                    <a href="<?php echo add_query_arg('post_action', 'dislike'); ?>">
                        Dislike (<?php echo get_post_like_count('dislikes') ?>)
                    </a>
                </li>

            </ul>
        
        <?php

        $output = ob_get_clean();

        return $output . $content;
    
    } else {
    
        return $content;
    }
}

function get_post_like_count( $type = 'likes' ) {

    // like or dislike count
    $current_count = get_post_meta( get_the_id(), $type, true );

    return ( $current_count ? $current_count : 0 );
}


add_action( 'template_redirect', 'process_post_like_click' );
function process_post_like_click() {

    $processed_like = false;
    $redirect = false;

    // Check if like or dislike
    if ( is_singular( 'post' ) ) {
        
        if ( isset( $_GET['post_action'] ) ) {
            
            if ( $_GET['post_action'] == 'like' ) {
            
                // Like
                $like_count = get_post_meta( get_the_id(), 'likes', true );

                if ( $like_count ) {
                    $like_count = $like_count + 1;
            
                } else {
                    $like_count = 1;
                }

                $processed_like = update_post_meta( get_the_id(), 'likes', $like_count );
            
            } elseif ( $_GET['post_action'] == 'dislike' ) {

                // Dislike
                $dislike_count = get_post_meta( get_the_id(), 'dislikes', true );

                if ( $dislike_count ) {
                    $dislike_count = $dislike_count + 1;
            
                } else {
                    $dislike_count = 1;
                }

                $processed_like = update_post_meta( get_the_id(), 'dislikes', $dislike_count );
            }

            if ( $processed_like ) {
                $redirect = get_the_permalink();
                
                // how do I deactivate both buttons on this post while keping them intact on other posts?

            }
        }
    }

    // redirect (refresh)
    if ( $redirect ) {
        wp_redirect( $redirect );
        die;
    }
}

Thanks all!

0
Dominic Herold 3 weeks 0 Answers 6 views 0

Leave an answer