the graphic design blog that speaks the truth

Below is a short explanation that will enable you to add a ShareThis button widget to your WordPress blog posts. The snippet below will work both on individual post pages and multi-post pages like categories, archives or custom page templates.

There are many plugins out there that will help you achieve this task but if you are anything like me, you like to self-contain your code as much as possible. It should be noted at this stage that this snippet will only work if you retain the link to the hosted version of the ShareThis javascript file. The only other piece of code you need is a span tag with some customized data fed into it. See below.

The Code

<script type="text/javascript" src="https://w.sharethis.com/button/buttons.js"></script>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
<span class='st_sharethis_hcount' displayText='ShareThis' st_url="<?php the_permalink(); ?>" 
st_title="<?php the_title();?>" st_image="<?php echo $image[0]; ?>"></span>

As you can see there are three important parameters here:

st_url: The link to the shared post

st_title: Displays the title

st_image: The URL of the post thumbnail

Using these parameters you should be able to customize this snippet to display whatever data you want it to. So there you have it, no need to install a plugin for this very simple task!

ABOUT THE AUTHOR

SHARE THIS ARTICLE

Voice Your Opinion

Thanks for your comment, it will appear here once it has been moderated.