Skip to Content
Menu

share()

Display more optimized and consistently styled, non-native social sharing buttons.

PHP February 14, 2019

Usage

PHP
nebula()->share($networks, $id);

Parameters

$networks
(Optional) (Array) An array of social networks to display
Default: None

$id
(Optional) (Integer) A post ID to share
Default: None

Parameter Notes

If no $id is provided, the current post is used.

Request or provide clarification »

Examples

PHP
<?php echo nebula()->share(array('shareapi', 'facebook', 'twitter', 'linkedin', 'pinterest', 'email')); ?>
Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

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

    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_share"
    Need a new filter hook? Request one here.

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

    Note: This function contains 1 to-do comment.

    PHP
            public function share($networks=array('shareapi', 'facebook', 'twitter'), $id=false){
                $override = apply_filters('pre_nebula_share', null, $networks, $id);
                if ( isset($override) ){return;}
    
                //@todo "Nebula" 0: Use null coalescing operator here if possible
                if ( empty($id) ){
                    $id = get_the_id();
                }
    
                $encoded_url = urlencode(get_permalink($id));
                $encoded_title = urlencode(get_the_title($id));
    
                //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 exists and 'shareapi' is requested, return *only* the Share API
                if ( isset($this->super->cookie['shareapi']) || in_array($networks, array('shareapi')) ){
                    $networks = array('shareapi');
                }
    
                foreach ( $networks as $network ){
                    //Share API
                    if ( in_array($network, array('shareapi')) ){
                        echo '<a class="nebula-share-btn nebula-share shareapi" href="#">' . __('Share', 'nebula') . '</a>';
                    }
    
                    //Facebook
                    if ( in_array($network, array('facebook', 'fb')) ){
                        echo '<a class="nebula-share-btn facebook" href="http://www.facebook.com/sharer.php?u=' . $encoded_url . '&t=' . $encoded_title . '" target="_blank" rel="noopener">' . __('Share', 'nebula') . '</a>';
                    }
    
                    //Twitter
                    if ( in_array($network, array('twitter')) ){
                        echo '<a class="nebula-share-btn twitter" href="https://twitter.com/intent/tweet?text=' . $encoded_title . '&url=' . $encoded_url . '" target="_blank" rel="noopener">' . __('Tweet', 'nebula') . '</a>';
                    }
    
                    //LinkedIn
                    if ( in_array($network, array('linkedin', 'li')) ){
                        echo '<a class="nebula-share-btn linkedin" href="http://www.linkedin.com/shareArticle?mini=true&url=' . $encoded_url . '&title=' . $encoded_title . '" target="_blank" rel="noopener">' . __('Share', 'nebula') . '</a>';
                    }
    
                    //Pinterest
                    if ( in_array($network, array('pinterest', 'pin')) ){
                        echo '<a class="nebula-share-btn pinterest" href="http://pinterest.com/pin/create/button/?url=' . $encoded_url . '" target="_blank" rel="noopener">' . __('Share', 'nebula') . '</a>';
                    }
    
                    //Email
                    if ( in_array($network, array('email')) ){
                        echo '<a class="nebula-share-btn email" href="mailto:?subject=' . $encoded_title . '&body=' . $encoded_url . '" target="_blank" rel="noopener">' . __('Email', 'nebula') . '</a>';
                    }
                }
    
                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_share', 'my_custom_share', 10, 3); //The last integer must be 1 more than the actual parameters
    function my_custom_share($null, $networks, $id){ //$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_share', '__return_false');