Skip to content

For Developers

Hooks, filters, and extension points available in Simple Post Like.

Overview

Simple Post Like is built with developers in mind. Beyond the spl_like_button() helper function and the CSS custom properties system, the plugin exposes a set of action hooks and filters that let you extend or modify its behavior without touching plugin files.


Action Hooks

spl_before_like

Fires just before a like is recorded in the database. Runs inside the AJAX handler after all validation has passed — post ID is valid, user has not already liked the post, and guest likes are permitted if applicable.

php

do_action( 'spl_before_like', int $post_id, int $user_id, string $user_ip_hash );
ParameterTypeDescription
$post_idintThe post being liked
$user_idintWordPress user ID — 0 for guests
$user_ip_hashstringSHA-256 hashed IP — empty string for logged-in users

Example — log a like event:

php

add_action( 'spl_before_like', function( int $post_id, int $user_id, string $user_ip_hash ): void {
    error_log( sprintf( 'Post %d liked by user %d', $post_id, $user_id ) );
}, 10, 3 );

spl_after_like

Fires immediately after a like has been successfully recorded and the like count updated.

php

do_action( 'spl_after_like', int $post_id, int $new_count, int $user_id );
ParameterTypeDescription
$post_idintThe post that was liked
$new_countintThe updated like count after this like
$user_idintWordPress user ID — 0 for guests

Example — trigger a notification on milestone:

php

add_action( 'spl_after_like', function( int $post_id, int $new_count, int $user_id ): void {
    if ( $new_count === 100 ) {
        // Send email to post author when a post reaches 100 likes.
        $author_email = get_the_author_meta( 'email', get_post_field( 'post_author', $post_id ) );
        wp_mail( $author_email, '🎉 Your post reached 100 likes!', get_permalink( $post_id ) );
    }
}, 10, 3 );

spl_after_unlike

Fires immediately after a like has been successfully removed and the like count updated.

php

do_action( 'spl_after_unlike', int $post_id, int $new_count, int $user_id );
ParameterTypeDescription
$post_idintThe post that was unliked
$new_countintThe updated like count after this unlike
$user_idintWordPress user ID — 0 for guests

Filter Hooks

spl_like_button_html

Filters the complete like button HTML before it is returned by LikeButton::get_like_button_html(). Use this to wrap the button, append elements, or replace the output entirely.

php

apply_filters( 'spl_like_button_html', string $html, int $post_id, string $display_type );
ParameterTypeDescription
$htmlstringThe complete like button HTML
$post_idintThe post ID the button belongs to
$display_typestringThe resolved display style being rendered

Example — wrap the button in a custom container:

php

add_filter( 'spl_like_button_html', function( string $html, int $post_id, string $display_type ): string {
    return '<div class="my-like-wrapper" data-style="' . esc_attr( $display_type ) . '">' . $html . '</div>';
}, 10, 3 );

spl_allowed_post_types

Filters the list of post types on which the like button is enabled. By default this returns the post types saved in Settings. Use this to programmatically add or remove post types without changing the Settings value.

php

apply_filters( 'spl_allowed_post_types', array $post_types );
ParameterTypeDescription
$post_typesarrayArray of post type slugs — e.g. ['post', 'page']

Example — always enable on a custom post type regardless of Settings:

php

add_filter( 'spl_allowed_post_types', function( array $post_types ): array {
    $post_types[] = 'portfolio';
    return array_unique( $post_types );
} );

spl_guest_likes_enabled

Filters whether guest likes are enabled. By default returns the Allow Guest Likes setting value. Use this to override the setting conditionally — for example, based on the current post type or user role.

php

apply_filters( 'spl_guest_likes_enabled', bool $enabled );
ParameterTypeDescription
$enabledboolWhether guest likes are currently enabled

Example — disable guest likes on a specific post type:

php

add_filter( 'spl_guest_likes_enabled', function( bool $enabled ): bool {
    if ( is_singular( 'review' ) ) {
        return false;
    }
    return $enabled;
} );

spl_like_count_format

Filters the formatted like count string before it is rendered in the button or returned via AJAX. By default, LikeButton::format_like_count() formats numbers as 1K, 1.5M etc. Use this filter to change the format.

php

apply_filters( 'spl_like_count_format', string $formatted, int $raw_count );
ParameterTypeDescription
$formattedstringThe formatted count string e.g. '1.2K'
$raw_countintThe raw integer like count

Example — always show the full number with no abbreviation:

php

add_filter( 'spl_like_count_format', function( string $formatted, int $raw_count ): string {
    return number_format( $raw_count );
}, 10, 2 );

Public API Summary

FunctionDescription
spl_like_button( $post_id, $style )Render and return the like button HTML
get_post_meta( $post_id, '_simple_post_like_count', true )Get raw like count for a post
get_post_meta( $post_id, '_simple_post_like_users', true )Get array of user IDs who liked a post
get_post_meta( $post_id, '_simple_post_like_ips', true )Get array of hashed IPs for guest likes

Architecture Notes

Simple Post Like follows a singleton pattern throughout. Every class uses the Singleton trait and is accessed via ClassName::instance(). If you need to interact with a class directly in custom code, use the instance accessor:

php

// Access Settings
$display_type = \FrontTheme\SimplePostLike\Settings::instance()->get( 'display_type' );

// Access LikeButton
$html = \FrontTheme\SimplePostLike\LikeButton::instance()->get_like_button_html( $post_id );

// Format a number using the plugin's formatter
$formatted = \FrontTheme\SimplePostLike\LikeButton::instance()->format_like_count( 1500 ); // → '1.5K'

Next Steps

On This Page