add_filter: swp_link_shortening

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

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

Description

This filter hook modifies the inbound URL that will be sent out on the various social networks allowing you to replace it with a shortened link. This filter runs immediately after add_filter: swp_analytics, allowing the URL to have analytics data attached to it prior to it being shortened and sent out to social media.

Example Usage

// Add your function to the filter
add_filter('swp_link_shortening' , 'swp_bitly_shortener' );

// Create your filter function
function swp_bitly_shortener( $array ) {

    // Collect the URL and social Network from the passed array
    $url = $array['url'];
    $network = $array['network'];

    // Fetch the User's Options 
    $options = swp_get_user_options();

    // Check if Link Shortening is even activated
    if($options['linkShortening'] == true) :

        // Check if Bitly is selected and that we have all the Bitly credentials
        if($options['shorteningMethod'] == 'bitly' 
            && isset($options['bitlyLogin']) 
            && isset($options['bitlyAPI'])):

            // Run your functions to modify the URL here

        endif;
    endif;

    // Return the modified URL or the world will explode
    return $url;
}

Parameters

This filter makes use of WordPress default add_filter function.

$array
(array) The add_filter function will pass one parameter containing an array with the URL ($array[‘url’]) to be modified and the name of the Social Network ($network[‘network’]) the URL is being created for.

Special Considerations

The beginning of any filter on this hooks should begin with two conditionals:

  • 1. First, check to ensure the link shortening is activated in the options panel.
  • 2. Second, check to ensure that your link shortening method is the one selected and that any necessary API or login credentials have been provided by the user.

This will ensure that the link is not being shortened in one filter, then passed to the next filter to be shortened again, and again, and so on. The link should only be shortened one time. Both of these conditionals are demonstrated in the example above.

Was this article helpful?

Related Articles