Socialize API

Extending Socialize: Adding a Custom Service

Socialize is built to be developer-friendly. If you want to add support for a social network or action not included by default, you can register a custom button using the socialize-get_services filter.

This filter allows you to define both inline and call-to-action (action) buttons with a custom rendering callback. Custom services must use unique numeric identifiers greater than 40 to avoid conflicts with core services.


Example: Adding Mastodon Support

class SocializeMastodon
{
    public static function createShareLink()
    {
        $socialize_settings = socializeWP::get_options();

        $svg_size = !empty($socialize_settings['socialize_svg_size']) ? $socialize_settings['socialize_svg_size'] : '20';
        $svg_color = !empty($socialize_settings['socialize_svg_color']) ? $socialize_settings['socialize_svg_color'] : '#000';

        $svg_path = SOCIALIZE_PATH . '/frontend/assets/mastodon.svg';
        $svg_content = file_exists($svg_path) ? file_get_contents($svg_path) : '';
        $svg_content = preg_replace(
            '/<svg([^>]+)>/',
            '<svg$1 width="' . $svg_size . '" height="' . $svg_size . '" fill="' . $svg_color . '">',
            $svg_content
        );

        $buttonCode = '<a href="https://yourdomain.com/share?url=' . urlencode(get_permalink()) . '" target="_blank" title="Share on Mastodon">';
        $buttonCode .= $svg_content;
        $buttonCode .= '</a>';

        return $buttonCode;
    }
}

add_filter('socialize-get_services', function ($services) {
    $services['Mastodon'] = array(
        'inline'  => 41, // Must be > 40
        'action'  => 42, // Must be > 40 and unique
        'callback' => array('SocializeMastodon', 'createShareLink')
    );
    return $services;
});

Notes

  • inline: Defines the ID for the button when shown inline (inside content).
  • action: Defines the ID for the button when used in the CTA box.
  • callback: Should return the complete HTML for the button. It must be a static method or function.

Reserved IDs

IDs 1 through 40 are reserved for built-in Socialize services. Custom services should use IDs starting from 41 and up to avoid conflicts.