Change the Order of the Sharing Buttons

A user recently asked if it would be possible to change the order of the sharing buttons in Scriptless Social Sharing, similar to how some other plugins handle it. As I looked, one issue I encountered is that other plugins accomplish reordering via a drag and drop system in the admin, which requires a script. Not sure if it’s cheating to add a script on the back end, but at this point, let’s say it is.

It’s still possible to change the order of the sharing buttons on your site, if you are willing and able to utilize a filter. Please remember to practice safe coding, back up your files, etc. Here are two options for sorting:

Option One: Sort the Buttons Alphabetically

This is the simplest option, and will automatically sort the buttons alphabetically:

<?php
add_filter( 'scriptlesssocialsharing_networks', 'prefix_sort_networks_alpha' );
/**
* A function to reorder the social networks alphabetically for Scriptless Social Sharing.
*
* @author Robin Cornett
* @param $networks
*
* @return mixed
*/
function prefix_sort_networks_alpha( $networks ) {
asort( $networks );
return $networks;
}

Option Two: Sort the Buttons By a Custom Order

This option is a bit more work, but completely under your control:

<?php
add_filter( 'scriptlesssocialsharing_networks', 'prefix_sort_networks_custom' );
/**
* A function to reorder the social networks for Scriptless Social Sharing.
* Set a value for each network order. 0 is first.
* @author Robin Cornett
* @param $networks
*
* @return mixed
*/
function prefix_sort_networks_custom( $networks ) {
$networks['email']['order'] = 7;
$networks['facebook']['order'] = 1;
$networks['google']['order'] = 2;
$networks['linkedin']['order'] = 4;
$networks['reddit']['order'] = 0;
$networks['twitter']['order'] = 6;
$networks['pinterest']['order'] = 5;
uasort( $networks, 'prefix_set_scriptless_sort_order' );
return $networks;
}
/**
* Custom comparison function to sort the networks.
* @param $a
* @param $b
*
* @return bool
*/
function prefix_set_scriptless_sort_order( $a, $b ) {
return $a['order'] > $b['order'];
}

Note that 0 comes first, and then higher numbers follow. Also note that if a new button is added to the plugin, it will default to 0 and end up at the beginning of your buttons list.

Return to Scriptless Social Sharing
Bars Code Check chevron-down chevron-left chevron-right chevron-up Envelope Laptop Search Times Facebook GitHub Google Plus Instagram LinkedIn Pinterest Twitter WordPress Logo Wordpress Simple