Skip to main content

How to Block own likes? 🚫

I know someone has a snippet out on blocking 🚫 own likes on posts.

Please share 🙏

Thank you

Ross FCA 

Shahjahan Jewel this is also a small feature request. Thank you much.

How to Block own likes? 🚫

Jacob Windham

<?php

if (!defined('ABSPATH')) exit;

add_filter('rest_pre_dispatch', 'fc_block_self_reactions_allow_bookmarks', 1, 3);

function fc_block_self_reactions_allow_bookmarks($result, $server, $request) {

    $current_user_id = get_current_user_id();
    if (!$current_user_id) {
        // Not logged in (or can't detect) – let WP/FC handle it.
        return $result;
    }

    $method = strtoupper($request->get_method());
    $route  = $request->get_route();

    global $wpdb;

    if (in_array($method, ['GET', 'HEAD', 'OPTIONS'], true)) {
        return null;
    }

    if (preg_match('#^/fluent-community/v2/feeds/(\d+)/react$#i', $route, $m)) {

        $post_id = (int) $m[1];

        $react_type = $request->get_param('react_type');
        $react_type = is_string($react_type) ? strtolower($react_type) : 'like';

        if ($react_type === 'bookmark') {
            return null;  // do NOT block bookmark on own post
        }

        $table = $wpdb->prefix . 'fcom_posts';

        $author_id = (int) $wpdb->get_var(
            $wpdb->prepare("SELECT user_id FROM `$table` WHERE id = %d LIMIT 1", $post_id)
        );

        if ($author_id === $current_user_id) {
            return new WP_Error(
                'fc_self_reaction_forbidden',
                __('You cannot react to your own post.', 'fluent-community'),
                ['status' => 422]
            );
        }

        return null;
    }

    if (preg_match('#^/fluent-community/v2/feeds/\d+/comments/(\d+)/reactions$#i', $route, $m)) {

        $comment_id = (int) $m[1];
        $table      = $wpdb->prefix . 'fcom_post_comments';

        $author_id = (int) $wpdb->get_var(
            $wpdb->prepare("SELECT user_id FROM `$table` WHERE id = %d LIMIT 1", $comment_id)
        );

        if ($author_id === $current_user_id) {
            return new WP_Error(
                'fc_self_comment_reaction_forbidden',
                __('You cannot react to your own comment.', 'fluent-community'),
                ['status' => 422]
            );
        }

        return null;
    }

    return null;
}

This is what I use on my site.

David Scurlock

Jacob Windham thank you so much.