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 );
| Parameter | Type | Description |
|---|---|---|
$post_id | int | The post being liked |
$user_id | int | WordPress user ID — 0 for guests |
$user_ip_hash | string | SHA-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 );
| Parameter | Type | Description |
|---|---|---|
$post_id | int | The post that was liked |
$new_count | int | The updated like count after this like |
$user_id | int | WordPress 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 );
| Parameter | Type | Description |
|---|---|---|
$post_id | int | The post that was unliked |
$new_count | int | The updated like count after this unlike |
$user_id | int | WordPress 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 );
| Parameter | Type | Description |
|---|---|---|
$html | string | The complete like button HTML |
$post_id | int | The post ID the button belongs to |
$display_type | string | The 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 );
| Parameter | Type | Description |
|---|---|---|
$post_types | array | Array 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 );
| Parameter | Type | Description |
|---|---|---|
$enabled | bool | Whether 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 );
| Parameter | Type | Description |
|---|---|---|
$formatted | string | The formatted count string e.g. '1.2K' |
$raw_count | int | The 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
| Function | Description |
|---|---|
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
- FAQ — common questions answered
- Troubleshooting — diagnosing and fixing common issues
- Statistics & Analytics — querying like data in custom templates