Create Shortcode for Jetpack Social Sharing Buttons

Jetpack have a lot of useful features that we can use on our WordPress powered websites. I usually use the social sharing features to enable my website visitors to share the post / page content to their own social media. By default the social share buttons is placed below the content of the post / page. You can move it to top or bottom or both of the post / page content.

For some projects, the website designer get creative and put the social sharing buttons inside the post content to improve the share-ability of the posts / pages. If you have the same problem, this snippet might help you a bit.

// Move Jetpack from the_content / the_excerpt using shortcode

function jptweak_remove_share() {
    remove_filter( 'the_content', 'sharing_display',19 );
    remove_filter( 'the_excerpt', 'sharing_display',19 );
    if ( class_exists( 'Jetpack_Likes' ) ) {
        remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
    }
}
 
add_action( 'loop_start', 'jptweak_remove_share' );

function oto_jetpack_share_func( $atts ){
	
	if ( function_exists( 'sharing_display' ) ) {
	    sharing_display( '', true );
	}
	 
	if ( class_exists( 'Jetpack_Likes' ) ) {
	    $custom_likes = new Jetpack_Likes;
	    return $custom_likes->post_likes( '' );
	}

}
add_shortcode( 'oto_jetpack_share', 'oto_jetpack_share_func' );

Using this snippet, I can put the social sharing buttons anywhere by using the [oto_jetpack_share] shortcode.

Happy coding!

Leave A Comment

%d bloggers like this: