PHP code example of alimir / wp-ulike

1. Go to this page and download the library: Download alimir/wp-ulike library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alimir / wp-ulike example snippets


echo do_shortcode('[wp_ulike for="post" id="123"]');

// Register custom template
add_filter('wp_ulike_add_templates_list', 'my_custom_template', 10, 1);
function my_custom_template($templates) {
    $templates['my-template'] = array(
        'name'            => 'My Custom Template',
        'callback'        => 'my_template_callback',
        'is_text_support' => true
    );
    return $templates;
}

// Custom template with full HTML control
function my_template_callback($args) {
    extract($args);
    ob_start();
    

// Get post likes
if (function_exists('wp_ulike_get_post_likes')):
    echo wp_ulike_get_post_likes(get_the_ID());
endif;

// Get comment likes
if (function_exists('wp_ulike_get_comment_likes')):
    echo wp_ulike_get_comment_likes(get_comment_ID());
endif;

// Get counter value (universal)
$count = wp_ulike_get_counter_value($item_id, 'post', 'like', true);

// Check if user liked an item
$is_liked = wp_ulike_is_user_liked($item_id, $user_id, 'likeThis');
// Returns count (0 if not liked, >0 if liked)

// Check user voting status from history
$args = array(
    'item_id'           => $item_id,
    'item_type'         => 'post',
    'current_user'      => get_current_user_id(),
    'settings'          => $settings,
    'is_user_logged_in' => is_user_logged_in()
);
$history = wp_ulike_get_user_item_history($args);
$user_status = isset($history[$item_id]) ? $history[$item_id] : false;

// Get most liked posts
$args = array(
    'type'     => 'post',
    'rel_type' => 'post',
    'status'   => 'like',
    'period'   => 'all',
    'limit'    => 10
);
$popular = wp_ulike_get_popular_items_info($args);

// Get popular item IDs only
$popular_ids = wp_ulike_get_popular_items_ids($args);

// Get total number of popular items
$total = wp_ulike_get_popular_items_total_number($args);

// Legacy function
$wp_query = wp_ulike_get_most_liked_posts(10, array('post'), 'post', 'all', 'like');

// Get user item history (all items user has liked)
$args = array(
    'item_id'           => 123,
    'item_type'         => 'post',
    'current_user'      => get_current_user_id(),
    'settings'          => $settings,
    'is_user_logged_in' => true
);
$history = wp_ulike_get_user_item_history($args);

// Check if user liked an item
$is_liked = wp_ulike_is_user_liked($item_id, $user_id, 'likeThis');

// Get user latest activity
$latest = wp_ulike_get_user_latest_activity($item_id, $user_id, 'post');

// Get user item count per day
$daily_count = wp_ulike_get_user_item_count_per_day($args);

// Get user data
$user_data = wp_ulike_get_user_data($user_id, array(
    'period' => 'all',
    'status' => array('like', 'dislike')
));

// Get likers list for an item
$likers = wp_ulike_get_likers_list_per_post($table_name, $column_name, $item_id, $limit);

// Get best likers (most active users)
$best_likers = wp_ulike_get_best_likers_info($limit, $period, $offset, $status);

// Get top engagers total number
$total_engagers = wp_ulike_get_top_enagers_total_number($period, $status);

// Get counter value info (with error handling)
$counter_info = wp_ulike_get_counter_value_info($item_id, 'post', 'like', true, $date_range);

// Format number (with K, M suffixes)
$formatted = wp_ulike_format_number(1234, 'like');

// Get button text
$button_text = wp_ulike_get_button_text('like', 'posts_group');

// Get post ID (with WPML support)
$post_id = wp_ulike_get_the_id($post_id);

// Check if WPML is active
$is_wpml = wp_ulike_is_wpml_active();

// Get rating value
$rating = wp_ulike_get_rating_value($post_id, true);


// Add meta data
wp_ulike_add_meta_data($object_id, $meta_group, $meta_key, $meta_value, $unique);

// Update meta data
wp_ulike_update_meta_data($object_id, $meta_group, $meta_key, $meta_value, $prev_value);

// Get meta data
$meta_value = wp_ulike_get_meta_data($object_id, $meta_group, $meta_key, $single);

// Delete meta data
wp_ulike_delete_meta_data($meta_group, $object_id, $meta_key, $meta_value, $delete_all);

// Update meta counter value
wp_ulike_update_meta_counter_value($ID, $value, $type, $status, $is_distinct, $prev_value);

// Get meta counter value
$counter = wp_ulike_meta_counter_value($ID, $type, $status, $is_distinct);

// Delete vote data
wp_ulike_delete_vote_data($ID, $type);

// Display like button for posts
wp_ulike('put', array('id' => 123));

// Display like button for comments
wp_ulike_comments('put', array('id' => 456));

// Display like button for BuddyPress activities
wp_ulike_buddypress('put', array('id' => 789));

// Display like button for bbPress topics
wp_ulike_bbpress('put', array('id' => 101));

// Get most liked comments
$comments = wp_ulike_get_most_liked_comments(10, 'post', 'all', 'like');

// Get most liked activities
$activities = wp_ulike_get_most_liked_activities(10, 'all', 'like');

add_action('wp_ulike_before_process', 'my_before_process_action', 10, 1);
function my_before_process_action($data) {
    // $data contains: id, type, nonce, factor, template, displayLikers, likersTemplate, client_address
    // Fired before the like/dislike process begins
    // Use this to validate, log, or modify data before processing
}

add_action('wp_ulike_after_process', 'my_after_process_action', 10, 6);
function my_after_process_action($id, $key, $user_id, $status, $has_log, $slug) {
    // Fired after the like/dislike process completes
    // $id = item ID, $key = item key, $user_id = user ID, $status = like/unlike status
    // $has_log = whether previous log exists, $slug = content type (post/comment/activity/topic)
    // Example: Send notifications, update external systems, etc.
}

add_action('wp_ulike_data_inserted', 'my_custom_action_on_like', 10, 1);
function my_custom_action_on_like($args) {
    // $args contains: id, item_id, table, related_column, type, user_id, status, ip
    // Example: Send email notification, update custom meta, etc.
    if ($args['status'] === 'like') {
        // Do something when user likes
    }
}

add_action('wp_ulike_data_updated', 'my_custom_action_on_update', 10, 1);
function my_custom_action_on_update($args) {
    // $args contains: item_id, table, related_column, type, user_id, status, ip
    // Example: Track status changes, update analytics, etc.
}

add_action('wp_ulike_counter_value_generated', 'my_counter_action');
function my_counter_action() {
    // Fired when counter value is calculated
}

add_action('wp_ulike_delete_vote_data', 'my_delete_action', 10, 3);
function my_delete_action($ID, $type, $settings) {
    // Fired when vote data is deleted
    // $ID = item ID, $type = content type, $settings = settings object
}

add_action('wp_ulike_loaded', 'my_plugin_loaded_action');
function my_plugin_loaded_action() {
    // Fired when plugin is fully loaded
}

add_action('wp_ulike_inline_display_likers_box', 'my_inline_likers', 10, 2);
function my_inline_likers($args, $get_settings) {
    // Customize inline likers box display
}

// These actions are fired for each meta group (post, comment, activity, topic, user, statistics)
// Replace {meta_group} with: post, comment, activity, topic, user, or statistics

// Before meta is added
add_action('wp_ulike_add_{meta_group}_meta', 'my_add_meta', 10, 4);
function my_add_meta($object_id, $meta_key, $meta_value, $unique) {
    // Fired before meta is added
}

// After meta is added
add_action('wp_ulike_added_{meta_group}_meta', 'my_added_meta', 10, 3);
function my_added_meta($mid, $object_id, $meta_key) {
    // Fired after meta is added
}

// Before meta is updated
add_action('wp_ulike_update_{meta_group}_meta', 'my_update_meta', 10, 3);
function my_update_meta($meta_id, $object_id, $meta_key) {
    // Fired before meta is updated
}

// After meta is updated
add_action('wp_ulike_updated_{meta_group}_meta', 'my_updated_meta', 10, 3);
function my_updated_meta($meta_id, $object_id, $meta_key) {
    // Fired after meta is updated
}

// Before meta is deleted
add_action('wp_ulike_delete_{meta_group}_meta', 'my_delete_meta', 10, 3);
function my_delete_meta($meta_ids, $object_id, $meta_key) {
    // Fired before meta is deleted
}

// After meta is deleted
add_action('wp_ulike_deleted_{meta_group}_meta', 'my_deleted_meta', 10, 3);
function my_deleted_meta($meta_ids, $object_id, $meta_key) {
    // Fired after meta is deleted
}

// Before template is rendered
add_action('wp_ulike_before_template', 'add_before_template_content', 10, 1);
function add_before_template_content($template_args) {
    // Add custom content before the like button
}

// Inside the like button
add_action('wp_ulike_inside_like_button', 'add_inside_button_content', 10, 1);

// After up vote button
add_action('wp_ulike_after_up_vote_button', 'add_after_button_content', 10, 1);

// Inside template wrapper
add_action('wp_ulike_inside_template', 'add_inside_template_content', 10, 1);

// After template is rendered
add_action('wp_ulike_after_template', 'add_after_template_content', 10, 1);

add_filter('wp_ulike_add_templates_args', 'modify_template_args', 10, 1);
function modify_template_args($args) {
    // Modify template arguments before rendering
    return $args;
}

add_filter('wp_ulike_return_final_templates', 'modify_final_template', 10, 2);
function modify_final_template($template, $args) {
    // Modify the final HTML output
    return $template;
}

add_filter('wp_ulike_count_box_template', 'custom_count_box', 10, 2);
function custom_count_box($string, $counter) {
    // Customize the count display HTML
    return '<span class="my-custom-count">' . $counter . '</span>';
}

add_filter('wp_ulike_login_alert_template', 'custom_login_alert');
function custom_login_alert() {
    return '<div class="alert">Please login to like this content!</div>';
}

add_filter('wp_ulike_button_text', 'custom_button_text', 10, 3);
function custom_button_text($value, $option_name, $setting_key) {
    // Customize button text based on context
    return $value;
}

add_filter('wp_ulike_custom_css', 'add_custom_styles');
function add_custom_styles($css) {
    return $css . '.wp_ulike_btn { border-radius: 20px; }';
}

add_filter('wp_ulike_the_content', 'modify_content_output', 10, 2);
function modify_content_output($output, $content) {
    // Modify the content with like button
    return $output;
}

add_filter('wp_ulike_comment_text', 'modify_comment_text', 10, 3);
function modify_comment_text($output, $content, $comment) {
    // Modify comment text with like button
    return $output;
}

add_filter('wp_ulike_format_number', 'custom_format_number', 10, 3);
function custom_format_number($value, $number, $status) {
    // Customize number formatting (e.g., 1K, 1M)
    return $value;
}

add_filter('wp_ulike_likers_box_shortcode', 'custom_likers_shortcode', 10, 3);
function custom_likers_shortcode($output, $id, $type) {
    // Customize likers box shortcode output
    return $output;
}

add_filter('wp_ulike_listener_data', 'modify_listener_data', 10, 1);
function modify_listener_data($data) {
    // Modify AJAX listener data before processing
    return $data;
}

add_filter('wp_ulike_get_the_id', 'modify_get_id', 10, 1);
function modify_get_id($post_id) {
    // Modify post ID retrieval (useful for WPML, etc.)
    return $post_id;
}

// Enable/disable cookie setting
add_filter('wp_ulike_set_cookie_enabled', 'control_cookie_setting', 10, 4);
function control_cookie_setting($enabled, $name, $value, $expire) {
    // Return false to prevent cookie from being set
    return $enabled;
}

// Modify cookie httponly flag
add_filter('wp_ulike_cookie_httponly', 'modify_cookie_httponly', 10, 4);
function modify_cookie_httponly($httponly, $name, $value, $expire) {
    return $httponly;
}

add_filter('wp_ulike_auto_diplay_filter_list', 'add_auto_display_filters', 10, 1);
function add_auto_display_filters($defaults) {
    // Add custom conditions for auto display
    $defaults['is_custom_page'] = is_page('custom-page');
    return $defaults;
}

add_filter('wp_ulike_supported_post_types_for_top_posts_list', 'add_post_types', 10, 1);
function add_post_types($post_types) {
    // Add custom post types to popular posts list
    $post_types[] = 'custom_post_type';
    return $post_types;
}

add_filter('wp_ulike_get_top_posts_query', 'modify_top_posts_query', 10, 1);
function modify_top_posts_query($args) {
    // Modify WP_Query arguments for top posts
    return $args;
}

add_filter('wp_ulike_get_top_comments_query', 'modify_top_comments_query', 10, 1);
function modify_top_comments_query($args) {
    // Modify WP_Comment_Query arguments for top comments
    return $args;
}

add_filter('wp_ulike_comments_add_attr', 'add_comment_attributes');
function add_comment_attributes($attributes) {
    return 'data-custom="value"';
}

add_filter('wp_ulike_activities_add_attr', 'add_activity_attributes');
function add_activity_attributes($attributes) {
    return 'data-custom="value"';
}

add_filter('wp_ulike_topics_add_attr', 'add_topic_attributes');
function add_topic_attributes($attributes) {
    return 'data-custom="value"';
}

// Posts microdata
add_filter('wp_ulike_posts_microdata', 'custom_posts_microdata');
function custom_posts_microdata($microdata) {
    return '<script type="application/ld+json">{"@type": "Article"}</script>';
}

// Comments microdata
add_filter('wp_ulike_comments_microdata', 'custom_comments_microdata');

// Activities microdata
add_filter('wp_ulike_activities_microdata', 'custom_activities_microdata');

// Topics microdata
add_filter('wp_ulike_topics_microdata', 'custom_topics_microdata');

add_filter('wp_ulike_posts_add_attr', 'add_custom_attributes');
function add_custom_attributes($attributes) {
    return 'data-custom="value"';
}

add_filter('wp_ulike_rating_value', 'customize_rating', 10, 2);
function customize_rating($rating_value, $post_ID) {
    // Customize rating calculation
    return $rating_value;
}

// These filters are available for each meta group
// Replace {meta_group} with: post, comment, activity, topic, user, or statistics

// Override meta add operation
add_filter('wp_ulike_add_{meta_group}_metadata', 'override_add_meta', 10, 5);
function override_add_meta($check, $object_id, $meta_key, $meta_value, $unique) {
    // Return non-null to override default behavior
    return $check;
}

// Override meta update operation
add_filter('wp_ulike_update_{meta_group}_metadata', 'override_update_meta', 10, 4);
function override_update_meta($check, $object_id, $meta_key, $meta_value, $prev_value) {
    return $check;
}

// Override meta delete operation
add_filter('wp_ulike_delete_{meta_group}_metadata', 'override_delete_meta', 10, 4);
function override_delete_meta($check, $object_id, $meta_key, $meta_value, $delete_all) {
    return $check;
}

// Default meta value
add_filter('wp_ulike_default_{meta_group}_metadata', 'default_meta_value', 10, 5);
function default_meta_value($value, $object_id, $meta_key, $single, $meta_group) {
    // Return default value if meta doesn't exist
    return $value;
}

add_filter('wp_ulike_permission_status', 'custom_permission_check', 10, 3);
function custom_permission_check($status, $args, $settings) {
    // Return true/false to allow/deny voting
    // $args contains item_id, type, user_id, etc.
    return $status;
}

// Note: This filter is referenced but implemented via wp_ulike_blacklist_validator class
// Use the permission status filter for custom validation

add_filter('wp_ulike_user_current_status', 'modify_user_status', 10, 3);
function modify_user_status($currentStatus, $prevStatus, $args) {
    // Modify the current user voting status
    return $currentStatus;
}

add_filter('wp_ulike_user_prev_status', 'modify_prev_status', 10, 2);
function modify_prev_status($prevStatus, $args) {
    // Modify the previous user voting status
    return $prevStatus;
}

add_filter('wp_ulike_ajax_respond', 'customize_ajax_response', 10, 4);
function customize_ajax_response($response, $item_id, $status, $atts) {
    // Modify AJAX response data
    $response['custom_data'] = 'my custom value';
    return $response;
}

add_filter('wp_ulike_ajax_counter_value', 'custom_ajax_counter', 10, 6);
function custom_ajax_counter($counter_val, $item_id, $item_type, $status, $is_distinct, $template) {
    // Modify counter value in AJAX response
    return $counter_val;
}

add_filter('wp_ulike_ajax_process_atts', 'modify_ajax_atts', 10, 1);
function modify_ajax_atts($atts) {
    // Modify AJAX process attributes
    return $atts;
}

// When user likes (status 1)
add_filter('wp_ulike_respond_for_not_liked_data', 'custom_like_response', 10, 2);

// When user unlikes (status 2)
add_filter('wp_ulike_respond_for_unliked_data', 'custom_unlike_response', 10, 2);

// When user re-likes (status 3)
add_filter('wp_ulike_respond_for_liked_data', 'custom_relike_response', 10, 2);

// When no limit mode (status 4)
add_filter('wp_ulike_respond_for_no_limit_data', 'custom_no_limit_response', 10, 2);

add_filter('wp_ulike_counter_value', 'modify_counter', 10, 5);
function modify_counter($counter_value, $ID, $type, $status, $date_range) {
    // Modify the counter value
    return $counter_value;
}

add_filter('wp_ulike_get_likers_list', 'customize_likers_list', 10, 3);
function customize_likers_list($output, $item_type, $item_ID) {
    // Customize the likers list output
    return $output;
}

add_filter('wp_ulike_get_likers_template', 'custom_likers_template', 10, 6);
function custom_likers_template($template, $get_users, $item_ID, $parsed_args, $table_name, $column_name) {
    // Return custom likers template HTML
    return $template;
}

add_filter('wp_ulike_get_user_ip', 'custom_user_ip', 10, 1);
function custom_user_ip($ip) {
    // Modify or override user IP detection
    return $ip;
}

add_filter('wp_ulike_enable_auto_display', 'control_auto_display', 10, 2);
function control_auto_display($auto_display, $type) {
    // Control auto display per content type
    return $auto_display;
}

add_filter('wp_ulike_display_capabilities', 'custom_display_caps', 10, 2);
function custom_display_caps($allowed_roles, $type) {
    // Modify which roles can see like buttons
    return $allowed_roles;
}

add_filter('wp_ulike_post_bp_notification_args', 'custom_bp_post_args', 10, 1);
add_filter('wp_ulike_comment_bp_notification_args', 'custom_bp_comment_args', 10, 1);
add_filter('wp_ulike_bp_add_notification_args', 'custom_bp_notification_args', 10, 1);

add_filter('wp_ulike_bp_notifications_template', 'custom_bp_template', 10, 4);
function custom_bp_template($content, $custom_link, $total_items, $item_id) {
    // Customize BuddyPress notification template
    return $content;
}

add_filter('wp_ulike_add_templates_list', 'add_custom_template', 10, 1);
function add_custom_template($templates) {
    $templates['my-custom-template'] = array(
        'name'            => 'My Custom Template',
        'callback'        => 'my_template_callback',
        'symbol'          => 'path/to/icon.svg',
        'is_text_support' => true
    );
    return $templates;
}

/**
 * Register custom template
 *
 * @param array $templates
 * @return array
 */
function wp_ulike_register_custom_template( $templates ) {
    $templates['wpulike-custom-template'] = array(
        'name'            => __('Custom Template', 'wp-ulike'),
        'callback'        => 'wp_ulike_custom_template_content',
        'symbol'          => WP_ULIKE_ASSETS_URL . '/img/svg/default.svg',
        'is_text_support' => true
    );
    return $templates;
}
add_filter( 'wp_ulike_add_templates_list', 'wp_ulike_register_custom_template', 10, 1 );

/**
 * Custom template content with full HTML control
 *
 * @param array $wp_ulike_template Template arguments array
 * @return string HTML output
 */
function wp_ulike_custom_template_content( array $wp_ulike_template ) {
    // Start output buffering
    ob_start();
    
    // Fire before template hook
    do_action( 'wp_ulike_before_template', $wp_ulike_template );
    
    // Extract template variables for easier access
    extract( $wp_ulike_template );
    
    // Available variables (extracted from $wp_ulike_template array):
    // $ID - Item ID
    // $type - Content type (post, comment, activity, topic) - same as $slug
    // $slug - Content type slug (post, comment, activity, topic)
    // $style - Template style name
    // $button_type - Button type: 'image' or 'text'
    // $wrapper_class - Additional CSS classes for wrapper
    // $general_class - General wrapper class (

// In your theme template files
echo do_shortcode('[wp_ulike for="post" style="wpulike-custom-template"]');

// Or using PHP function
wp_ulike('put', array(
    'id'    => get_the_ID(),
    'style' => 'wpulike-custom-template',
    'wrapper_class' => 'my-custom-class'
));

// In custom HTML structure

function wp_ulike_horizontal_template( array $wp_ulike_template ) {
    ob_start();
    extract( $wp_ulike_template );
    

add_filter('wp_ulike_button_selector', 'custom_button_selector', 10, 1);
function custom_button_selector($selector) {
    return 'my-custom-button-class';
}

add_filter('wp_ulike_general_selector', 'custom_general_selector', 10, 1);
function custom_general_selector($selector) {
    return 'my-custom-wrapper-class';
}

add_filter('wp_ulike_method_id', 'custom_method_id', 10, 2);
function custom_method_id($method_id, $instance) {
    // Customize the method ID
    return $method_id;
}

[wp_ulike for="post"]
[wp_ulike for="comment" style="wpulike-heart"]
[wp_ulike for="post" id="123" button_type="text"]