Rendering and Tracking Hooks

Developer Knowledge Level

This content is intended for WordPress developers, and it may require coding knowledge of WordPress, PHP, and JavaScript. Code examples provided here may contain errors or needs some additional coding. Make sure to test the code before using it on a live website!

These hooks are available from coreSocial 4.0. Read the Developer Guide for registration timing and the existing placement and page-context hooks. PHP filters must return their value; actions observe an event and do not replace processing.

Select networks for an item

coresocial_method_networks receives the selected network codes, method name, item context, and method instance. The context contains item, item_id, url, title, and image. Method names are inline, floating, and explore.

The filter runs after normal selection and before button construction or online-count queueing. Return an ordered array of codes; an empty array renders no buttons. Duplicates, unavailable networks, and codes from the wrong AI/social group are ignored. A non-array return preserves the original selection. Pro count sorting can subsequently reorder the selected buttons.

add_filter( 'coresocial_method_networks', function ( $codes, $method, $context ) {
    if ( $method === 'inline' && $context['item'] === 'post'
        && get_post_type( $context['item_id'] ) === 'product' ) {
        return array( 'pinterest', 'facebook' );
    }
    return $codes;
}, 10, 3 );

The returned networks must already be enabled and available in the installed edition. This filter does not register new providers or enable premium features.

Customize a button

coresocial_button_args receives the merged argument array for every shared button renderer, including manual output, blocks, widgets, and floating buttons. It runs before tracking signatures and HTML escaping, after count sorting.

Use url for the outgoing share destination and preserve real for the canonical URL used in tracking. The array also contains network, module, item, item_id, title, label, name, accessible_label, popup_width, popup_height, count-display options, and a data map for additional data attributes. Preserve unrelated arguments and do not duplicate reserved tracking attributes in data. Standard URL escaping still applies; arbitrary app-link schemes are not enabled by a filter.

add_filter( 'coresocial_button_args', function ( $args ) {
    if ( $args['network'] === 'reddit' ) {
        $args['accessible_label'] = 'Discuss this article on Reddit';
        $args['data']['campaign'] = 'article-footer';
    }
    return $args;
} );

Supply dynamic profile data

coresocial_profiles receives the raw profiles keyed by profile ID and the profiles service. It runs on each list lookup and is used by profile selections and rendering. Each entry must provide id, network, name, and url; count and label are optional. Use supported profile network codes. The filter does not save changes to settings.

coresocial_profile receives the raw profile or false, the requested ID, and the profiles service. Return false to hide an entry. It runs before count defaults, brand lookup, and the mailto:, tel:, or skype: prefixes are applied. Email, phone, and Skype values should therefore use the same unprefixed format as their stored settings. Changes are resolved on each call and are not cached into stored profiles. This hook changes display data; the list filter determines the names offered in selection controls.

add_filter( 'coresocial_profile', function ( $profile, $id ) {
    if ( is_array( $profile ) && $id === 12 ) {
        $profile['count'] = 1250;
    }
    return $profile;
}, 10, 2 );

Customize an Explore prompt

Explore requires coreSocial Pro.

coresocial_explore_prompt receives the expanded prompt, AI provider code, and item context. It runs once per prepared provider button after template placeholders are expanded. Its plain-text result supplies both the destination link and the copy-panel prompt. Markup is stripped after filtering. Calling the low-level build_prompt() or get_share_link() directly does not run this preparation hook.

add_filter( 'coresocial_explore_prompt', function ( $prompt, $provider, $context ) {
    return $prompt . ' Reply in French.';
}, 10, 3 );

Observe recorded interactions

coresocial_tracking_recorded receives the interaction input, internal item database ID, and a boolean indicating whether an individual activity row was stored. The input includes action, network, module, item, item_id, url, logged, and user_id; likes also include user_hash. A validated AJAX input can additionally contain a tracking signature. Keep these values private and avoid copying the entire input into analytics logs.

The action fires after a new aggregate count is written, including when activity logging is disabled. It covers share, show, Explore, and Like interactions. Duplicate likes, failed item lookup, failed lock acquisition, failed activity inserts, and failed count writes do not fire it. For likes, the action runs before the duplicate-protection lock is released; keep listeners lightweight. It runs before the AJAX handler refreshes its count cache, so use a fresh database read if a listener needs the new aggregate.

The ID is a row ID in coreSocial’s items table, not the WordPress post or term ID. The database write methods add_share_to_log(), add_like_to_log(), update_counts(), and update_online_count() now return boolean outcomes. An interaction event does not prove completion on an external website, and the activity/count writes are not an atomic transaction.

Observe online count refreshes

coresocial_online_count_result receives the provider result and queued item after the item’s stored permalink has been refreshed. Return an array containing status, count for a successful result, and optional error, url, and network. A successful status is the exact string ok; a stored count must be numeric and is clamped to a nonnegative integer. A non-array return leaves the provider result unchanged.

coresocial_online_count_complete receives the filtered result, queued item, and a boolean indicating whether storage succeeded. It fires for available providers after processing, including provider failures, invalid results, and database failures. A provider reporting ok does not alone mean the count was stored. Jobs skipped because a provider was removed or disabled do not fire this action. These hooks do not change the normal count-refresh schedule.

Observe browser outcomes

JavaScript listeners use wp.hooks.addAction(). The new events are:

  • coresocial-tracking-success: (result, link) after a successful tracking response and before count display changes. It fires even when counts are hidden, zero, or the original button has been removed. The result contains status and can contain count and uid.
  • coresocial-tracking-error: (error, link) for a failed fetch, invalid JSON, HTTP error, or rejected tracking response. The original console error remains.
  • coresocial-sharing-copy-success: (link) when ClipboardJS reports a successful Copy URL operation, after the icon feedback is scheduled.
  • coresocial-sharing-copy-error: (link) when ClipboardJS reports a failed Copy URL operation.

Clipboard events cover Copy URL buttons, including buttons inserted after initialization. They do not report AI prompt copying or a missing ClipboardJS dependency. Tracking and clipboard outcomes are independent: a tracking request can succeed even when clipboard permission is denied. A tracking success means the endpoint returned OK; it does not establish a newly recorded like or completion of an external share.

wp.hooks.addAction(
    'coresocial-tracking-success',
    'my-site/tracking-observer',
    function ( result, link ) {
        console.log( link.dataset.network, result.count );
    }
);

Enqueue the listener with dependencies on wp-hooks and coresocial-main, before the interaction to observe. Keep listeners synchronous and lightweight, and handle their own errors. A listener exception is not reported as a tracking-request failure. Use addAction() for observers; wp.coresocial.core.registerAction() registers a replacement click handler instead.

Edit TypeScript in src, then run npm run typecheck, npm run lint:js -- src/frontend/index.ts, and npm run build from the plugin directory. The browser runs the compiled files in build.

Rate this article
0
0
2

You are not allowed to rate this post.

Leave a Comment

0
0
0
0
0
0
0
0
0