I recently installed the WordPress plugin Jetpack, which puts together some fabulous WP plugins into a bundle. The problem I was having was that Sharedaddy, one of the available bundled plugins, was not being shown in my posts. So I started looking around in the WP plugin forums, and really didn’t find a solution. I almost felt like I had to contact ‘support’ in order to get this working because a lot of the threads posted had such replies.

Anyways, after a little fiddling around and a tip in a thread about a function, I decided to go ahead and code it into my themes functions.php file.

If this breaks your WordPress – don’t blame me, I will attempt to be as clear as possible.

First open up your themes functions.php file, and open up the file at jetpack/modules/sharedaddy/sharing-service.php

If you’re familiar with the functions.php file, you know that this file has functions and miscellaneous stuff related specifically to your theme. The sharing-service.php file has PHP classes which set up the Sharedaddy services for use.

In your functions.php file create a new function for use within the theme. You can name the function anything you want. Go ahead and use this code.

 
function show_sharing($text = '') {
 
	include(ABSPATH . 'wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php');
 
	static $shared_with_posts = array();
	global $post;
 
	$server = new Sharing_Service;
	$global = $server->get_global_options();
 
	if ( !is_feed() ) {
		if ( $global['show'] == 'posts' && ( is_single() || is_page() ) )
			$show = true;
		elseif ( $global['show'] == 'index' && ( is_home() || is_archive() || is_search() ) )
			$show = true;
		elseif ( $global['show'] == 'posts-index' && ( is_single() || is_page() || is_home() || is_search() || is_archive() ) )
			$show = true;
	}
 
	$switched_status = get_post_meta( $post->ID, 'sharing_disabled', false );
 
	if ( !empty( $switched_status ) )
	{
		$show = false;
	}
 
	// Only show once
	if ( isset( $shared_with_posts[$post->ID] ) )
		$show = false;
 
	$shared_with_posts[$post->ID] = true;
	$sharing_content = '';
 
	if($show)
	{
		$enabled = $server->get_blog_services();
 
		if ( count( $enabled['all'] ) > 0 ) {
			global $post;
 
			$dir = get_option( 'text_direction' );
 
			// Wrapper
			$sharing_content .= '<div class="snap_nopreview sharing robots-nocontent">';
			$sharing_content .= '<ul>';
 
			// Visible items
			$visible = '';
			foreach ( $enabled['visible'] AS $id => $service ) {
				// Individual HTML for sharing service
				$visible .= '<li class="share-'.$service->get_class().' share-regular">';
				$visible .= $service->get_display( $post );
				$visible .= '</li>';
			}
 
			$parts = array();
			if ( $global['sharing_label'] != '' )
				$parts[] = '<li class="sharing_label">'.$global['sharing_label'].'</li>';
 
			$parts[] = $visible;
			if ( count( $enabled['hidden'] ) > 0 )
				$parts[] = '<li class="share-custom"><a href="#" class="sharing-anchor">'._x( 'Share', 'dropdown button', 'jetpack' ).'</a></li>';
 
			if ( $dir == 'rtl' )
				$parts = array_reverse( $parts );
 
			$sharing_content .= implode( '', $parts );			
			$sharing_content .= '<li class="share-end"></li></ul>';
 
			if ( count( $enabled['hidden'] ) > 0 ) {
				$sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;';
 
				if ( count( $enabled['hidden'] ) == 1 )
					$sharing_content .= 'width:150px;';
 
				$sharing_content .= '">';
 
				if ( count( $enabled['hidden'] ) == 1 )
					$sharing_content .= '<ul style="background-image:none;">';
				else
					$sharing_content .= '<ul>';
 
				$count = 1;
				foreach ( $enabled['hidden'] AS $id => $service ) {
					// Individual HTML for sharing service
					$sharing_content .= '<li class="share-'.$service->get_class().'">';
					$sharing_content .= $service->get_display( $post );
					$sharing_content .= '</li>';
 
					if ( ( $count % 2 ) == 0 )
						$sharing_content .= '<li class="share-end"></li>';
 
					$count ++;
				}
 
				// End of wrapper
				$sharing_content .= '<li class="share-end"></li></ul></div></div>';
			}
 
			$sharing_content .= '<div class="sharing-clear"></div></div>';
		}
 
	}
 
	echo $sharing_content;
}

Ok. Basically this function recreates another function found in the sharing-service.php file called sharing_display(), except that it is trimmed up for our purposes…I repeat – dirty hacks. :p

Now that you have a new function in your theme that rebuilds the Sharedaddy services bar, you can add it to any part of your theme with

echo show_sharing();

Celebrate by sharing this link with others

, , , , , , , ,
Trackback

17 comments untill now

  1. Wow this is a small world! I was having issues with sharedaddy on a custom theme I’ve been building and after a bit of searching and came across that. If you don’t remember me we met at the end of November on Forrst and you were working on your Code Igniter CMS admin panel.

    Anyways, it’s nice to see you again. We should keep in touch :)

  2. Hey Jay!

    Yes I do remember meeting you at Forrst :) …my admin panel a project that got pushed back, lol. Glad you were able to find my site, and this little hack. Its not much but it seems to work ;)

  3. Cool, the hack didn’t help me so much but it did help me get started. Once you mentioned the sharing-service.php I knew where to start, poked around to figure how it all worked. Then realized in my functions file I created a simple function for using the excerpt as a posts’s meta description tag for search engines and when used on sharing things via facebook.

  4. Oops I mean the function that did that in my functions.php was calling the excerpt filters which was triggering the sharedaddy hooks prematurely. So I just skipped calling those hooks for the meta description.

  5. Ok where do I put the — echo show_sharing(); bit of code. I added the other stuff to the functions.php file.

  6. Hi Mark,

    The echo show_sharing(); code will go into your single.php file (Single-Post).

    The Code

  7. Great post. It didn’t work for me. I get an error at the place I’m using the echo:


    Fatal error: Cannot redeclare sharing_add_footer() (previously declared in /home/httpd/vhosts/mypage/httpdocs/wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php:338) in /home/httpd/vhosts/mypage/httpdocs/wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php on line 346

    What does that mean?

  8. Hi Rene,

    a function is being called twice, in this case you should remove:
    wp_register_script('sharing-js', plugin_dir_url(__FILE__).'sharing-js', array( ' jquery' , '0.1' );
    add_action( 'wp_footer', 'sharing_add_footer');

    from the bottom of the function you created in your template functions file.

  9. Thanks for this post! I also did all the steps here and getting the same error as Rene. When I look at my edited functions.php file, I don’t see the CODE you say we should be removing. Am I looking at the right file? Thanks so much for your help!

  10. you guys are amazing.
    I couldn’t get this to work no matter how.

    my level of web programming is almost 0, but I can get away with a bunch of stuff.

    reading this post helped me immensely.

    thank you very much.

    I could get the function to work.

  11. the only thing that I had to do in order to work was remove the first line

    include(ABSPATH . 'wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php');

    if I have that line there, I get the following error.

    Fatal error: Cannot redeclare sharing_add_footer() (previously declared in /home/content/92/7946492/html/wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php:338) in /home/content/92/7946492/html/wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php on line 346.

    then I followed the instructions to remove the function being called twice and I get the error:
    Fatal error: Cannot redeclare sharing_add_footer() (previously declared in /home/content/92/7946492/html/wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php:338) in /home/content/92/7946492/html/wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php on line 346

    so I will remove the line:
    include(ABSPATH . 'wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php');

    because my sharing seems to be working.
    I don’t know exactly what this line : ”
    include(ABSPATH . ‘wp-content/plugins/jetpack/modules/sharedaddy/sharing-service.php’); ”
    does.

    Am I gonna have some troubles leaving it behind?

  12. Hi CircuitBomb, thanks for the guide! Will try this later because it is really bugging me and sharedaddy is the last piece of the puzzle for me (just starting my website) .
    2 questions:

    1: I have the sharedaddy plugin and not jetpack, do I simply replace the file path in the code?

    2: I am writing this from my phone and your mobile site looks fab!! Is that a plugin?

    Thx and again great guide.

    Karl

  13. Hi Jay! Could you describe what you did in more detail? I’d love to get this working but am running into the fatal error code described below.. Would really appreciate it! :)

  14. Hey Karl thanks for stopping by!

    I’m not sure where differences may lie between Jetpack and the actual Sharedaddy plugin, aside from directory structure. Right now I’m using Jetpack version 1.1.2 if that helps.

    The mobile them is a plugin and I love it, its called WP-Touch and you can get it here:http://wordpress.org/extend/plugins/wptouch/

  15. When I put the code in function php and in the single post php, the code after that disappear… What’s the problem?

  16. Tony Matt @ 2011-08-07 22:45

    I too did what Marioamadeu did and it is seems to have solved the problem. That bad part is I do not know what that line did which I now commented out.

    I will check out more later to verify that things are working correctly….

  17. I’ve created some straightforward instructions on how to add 300+ Specific Sharing Services to Sharedaddy and Jetpack. Let me know if you find it useful!

    Instructions:
    http://blog.shareaholic.com/2011/04/how-to-adding-specific-sharing-services-to-wordpress-com-sharedaddy-or-jetpack/

Add your comment now