Skip to Content

social()

A convenient way to implement social media “like”, “share”, and “tweet” buttons for Facebook, Twitter, LinkedIn, and Pinterest.

PHP February 14, 2019

Usage

PHP
nebula()->social($networks, $counts)

Parameters

$networks
(Optional) (String or Array) An array of which networks' buttons to use.
Default: Facebook, Twitter

$counts
(Optional) (Boolean) Whether to show the number of "likes", "shares", etc.
Default: false

Request or provide clarification »

Examples

Using a single network

PHP
<?php nebula()->social('facebook'); ?>

Only show "like" counts to developers and clients.

PHP
<?php nebula()->social(array('facebook', 'twitter'), nebula()->is_staff()); ?>

Demo


Source File

Located in /libs/Functions.php on line 1010.

1 Hook

Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

Filters
"pre_nebula_social"
Need a new filter hook? Request one here.

Actions
This function has no action hooks available. Request one?

PHP
    public function social($networks=array('shareapi', 'facebook', 'twitter'), $counts=0){
        $override = apply_filters('pre_nebula_social', null, $networks, $counts);
        if ( isset($override) ){return;}

        if ( is_string($networks) ){ //if $networks is a string, create an array for the string.
            $networks = array($networks);
        } elseif ( is_int($networks) && ($networks === 1 || $networks === 0) ){ //If it is an integer of 1 or 0, then set it to $counts
            $counts = $networks;
            $networks = array('shareapi', 'facebook', 'twitter');
        } elseif ( !is_array($networks) ){
            $networks = array('shareapi', 'facebook', 'twitter');
        }

        //Convert $networks to lower case without dashes/spaces for more flexible string matching later.
        $networks = array_map(function($value){
            return str_replace(array(' ', '_', '-'), '', strtolower($value));
        }, $networks);

        echo '<div class="sharing-links">';

        //If the 'shareapi' cookie and 'shareapi' is requested, return *only* the Share API
        if ( isset($_COOKIE['shareapi']) || in_array($networks, array('shareapi')) ){
            $networks = array('shareapi');
        }

        foreach ( $networks as $network ){
            //Share API
            if ( in_array($network, array('shareapi')) ){
                $this->share_api();
            }

            //Facebook
            if ( in_array($network, array('facebook', 'fb')) ){
                $this->facebook_share($counts);
            }

            //Twitter
            if ( in_array($network, array('twitter')) ){
                $this->twitter_tweet($counts);
            }

            //LinkedIn
            if ( in_array($network, array('linkedin', 'li')) ){
                $this->linkedin_share($counts);
            }

            //Pinterest
            if ( in_array($network, array('pinterest', 'pin')) ){
                $this->pinterest_pin($counts);
            }
        }

        echo '</div><!--/sharing-links-->';
    }

Override

To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

PHP
add_filter('pre_nebula_social', 'my_custom_social', 10, 3); //The last integer must be 1 more than the actual parameters
function my_custom_social($null, $networks, $counts){ //$null is required, but can be ignored
    //Write your own code here

    return true; //Return true to prevent the original function from running afterwords
}

You can completely disable this PHP function with a single line actions:

PHP
 add_filter('pre_nebula_social', '__return_false');