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 1226.

    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?

    PHP
            public function share($networks=array('shareapi', 'facebook', 'twitter'), $id=null){
                $override = apply_filters('pre_nebula_share', null, $networks, $id);
                if ( isset($override) ){return;}
    
                $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 '<ul class="sharing-links">';
    
                foreach ( $networks as $network ){
                    //Share API
                    if ( in_array($network, array('shareapi')) ){
                        echo '<li class="hidden"><a class="nebula-share-btn nebula-share shareapi" href="#" title="Share this post"><i class="fa-solid fa-share-nodes me-1"></i>' . __('Share', 'nebula') . '</a></li>';
                    }
    
                    //Facebook
                    if ( in_array($network, array('facebook', 'fb')) ){
                        echo '<li><a class="nebula-share-btn facebook" href="http://www.facebook.com/sharer.php?u=' . $encoded_url . '&t=' . $encoded_title . '" target="_blank" rel="noopener" title="' . __('Share on Facebook', 'nebula') . '" role="listitem"><i class="fa-brands fa-facebook me-1"></i>' . __('Share', 'nebula') . '</a></li>';
                    }
    
                    //Twitter/X
                    if ( in_array($network, array('twitter', 'x')) ){
                        echo '<li><a class="nebula-share-btn twitter" href="https://twitter.com/intent/tweet?text=' . $encoded_title . '&url=' . $encoded_url . '" target="_blank" rel="noopener" title="' . __('Share on X', 'nebula') . '" role="listitem"><i class="fa-brands fa-x-twitter me-1"></i></i>' . __('Post', 'nebula') . '</a></li>';
                    }
    
                    //LinkedIn
                    if ( in_array($network, array('linkedin', 'li')) ){
                        echo '<li><a class="nebula-share-btn linkedin" href="http://www.linkedin.com/shareArticle?mini=true&url=' . $encoded_url . '&title=' . $encoded_title . '" target="_blank" rel="noopener" title="' . __('Share on LinkedIn', 'nebula') . '" role="listitem"><i class="fa-brands fa-linkedin me-1" aria-hidden="true"></i>' . __('Share', 'nebula') . '</a></li>';
                    }
    
                    //Pinterest
                    if ( in_array($network, array('pinterest', 'pin')) ){
                        echo '<li><a class="nebula-share-btn pinterest" href="http://pinterest.com/pin/create/button/?url=' . $encoded_url . '" target="_blank" rel="noopener" title="' . __('Share on Pinterest', 'nebula') . '" role="listitem"><i class="fa-brands fa-pinterest me-1" aria-hidden="true"></i>' . __('Share', 'nebula') . '</a></li>';
                    }
    
                    //Email
                    if ( in_array($network, array('email')) ){
                        echo '<li><a class="nebula-share-btn email" href="mailto:?subject=' . $encoded_title . '&body=' . $encoded_url . '" target="_blank" rel="noopener" title="' . __('Share by email', 'nebula') . '" role="listitem"><i class="fa-solid fa-envelope me-1" aria-hidden="true"></i>' . __('Email', 'nebula') . '</a></li>';
                    }
                }
    
                echo '</ul><!--/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');