add_filter: swp_add_networks

  1. Home
  2. Knowledge Base
  3. Developers
  4. add_filter: swp_add_networks

Not the solution you were looking for?

Click the link below to submit a support ticket. If you are a Social Warfare - Pro license holder, you can log into your Account page to submit a ticket.
Submit Ticket


This filter hook modifies the list of networks for which Social Warfare will fetch share counts. The filter is passed an array of existing networks that are already queued. This array can be modified and then returned with the new social networks added. Your addon MUST include a function that can be called to fetch the share counts. The exact nature of your share count function will be detailed below.


Prior to adding your network to the queue for processing, you must add an option using add_filter: swp_options or Function: swp_add_option_after() to allow users to turn your network button on and off. The array key used to declare that option must match exactly to the network value you add to this filter and to the nomenclature of the share count collection function.

add_filter('swp_options', 'swp_options_function',10);
function swp_options_function($swp_options) {

    // Create the new option in a variable to be inserted
    $reddit = array(
        'reddit' => array(
             'type' => 'checkbox',
             'content' => 'Reddit',
             'default' => 1
    // Call the swp_add_option_after() function
    return swp_add_option_after($swp_options,'displaySettings','facebook',$reddit);


Example Usage

// Queue up your filter to be ran on the swp_options hook.
add_filter('swp_add_networks', 'your_plugin_network_function');

// Create the function that will filter the options
function your_plugin_network_function($networks) {
    // Add your network to the existing network array
    $networks[] = 'reddit';

    // Be sure to return the modified options array or the world will explode
    return $networks;

Required Share Count Function

You add-on must have a function designed to collect the share count information from that social network’s API. There are several requirements for this function:

  • Naming Convention: The function must be named in the following format: swp_fetch_reddit_shares($url) where “reddit” is the name that you passed into the network array above.
  • Arguments: The function must accept one argument: the URL.
  • Return: The function must return an integer containing the share count.

Here’s is an example function taken from the Social Warfare core:

function swp_fetch_twitter_shares($url) {
    $url = rawurlencode($url);
    $json_string = swp_file_get_contents_curl('' . $url);
    $json = json_decode($json_string, true);
    return isset($json['count'])?intval($json['count']):0;

It is recommended that you pass your data through the swp_file_get_contents_curl() function that is built into Social Warfare. This takes care of a multitude of cURL function across many different server configurations allowing the API request to go through without issue.

Advanced Demonstration

With your network queue up for processing and a properly set up share count fetching function in place, your network’s shares will be collected, cached, and passed to the buttons for display.

The following code demonstrates how these functions will be processed by Social Warfare:

// Loop through the networks and fetch their share counts
 foreach($networks as $network):
    // Check if this network is even activated
    if( $options[$network] ):
        // Check if we can used the cached share numbers
        if($freshCache == true):
            // If the cache is fresh we don't fetch new counts
        // If cache is expired, fetch new and update the cache
            // This is where we call your function to fetch the share counts
            $shares[$network] = call_user_func('swp_fetch_'.$network.'_shares',$url);
            // We do some more stuff here to update the cached counts

        $shares['totes'] += $shares[$network];

Was this article helpful?

Related Articles