Add a Facebook Messenger Button

It is possible to add your own custom sharing buttons to Scriptless Social Sharing, if the one you want doesn’t exist yet. This code shows you how to add a button for Facebook Messenger to your site.

First, register Facebook Messenger as an allowed protocol for links:

add_filter( 'kses_allowed_protocols', 'prefix_allow_messenger' );
/**
 * Add Facebook Messenger to the list of allowed URL protocols in WordPress.
 *
 * @param $protocols
 *
 * @return array
 */
function prefix_allow_sms( $protocols ) {
	$protocols[] = 'fb-messenger';

	return $protocols;
}Code language: PHP (php)

Then add Facebook Messenger to the list of buttons available to the plugin:

add_filter( 'scriptlesssocialsharing_networks', 'prefix_scriptless_networks' );
/**
 * Add FB Messenger to settings/allowed sharing buttons
 *
 * @param $networks
 *
 * @return mixed
 */
function prefix_scriptless_networks( $networks ) {
	$networks['messenger'] = array(
		'name'  => 'messenger',
		'label' => __( 'Facebook Messenger', 'scriptless-social-sharing' ),
		'icon'  => 'f39f',
		'color' => '#333', // change this to whatever color you want the button to be
	);

	return $networks;
}Code language: PHP (php)

Then define the URL for the Messenger button. Please note that you will have to create and add your own app ID ($app_id) for the URL to use. Also note that the hook here is scriptlesssocialsharing_messenger_url, but you will want to replace messenger with whatever you are using as the name for your button.

add_filter( 'scriptlesssocialsharing_messenger_url', 'prefix_add_fb_messenger', 10, 3 );
/**
 * Add SMS to settings/allowed sharing buttons
 *
 * @param $url
 * @param $button
 * @param $attributes
 *
 * @return mixed
 */
function prefix_add_fb_messenger( $url, $button, $attributes ) {
	$app_id = ''; // enter your fb app ID here
	if ( ! $app_id ) {
		return $url;
	}

	$url = add_query_arg(
		array(
			'link'   => $attributes['permalink'],
			'app_id' => $app_id,
		),
		'fb-messenger://share/'
	);

	return $url;
}Code language: PHP (php)

These instructions are specifically for Facebook Messenger, but you can use the general concepts for any kind of sharing button you would like to create. If your preferred button uses a standard link, then you can skip the first filter–that’s just needed here because Facebook Messenger uses a different protocol for launching the link.

Return to Scriptless Social Sharing