WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In developing custom themes I have clients who want to use ShareThis for their social media. ShareThis offers a plug-in with a control panel that lets the client manage the buttons, style, etc. the trouble is that the ShareThis plug-in renders the buttons using a filter on the_content. This means that the button must always appear right after the main content block. Is there some way I can manually control placement of the ShareThis buttons in my templates when using the plug-in? I know that I could manually place the ShareThis code in my templates, but then the client wouldn't have control inside the Wordpress Control panel.

share|improve this question

closed as off-topic by Pieter Goosen, Wyck, cybmeta, Johannes Pille, TheDeadMedic Nov 8 '14 at 12:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Pieter Goosen, Wyck, cybmeta, Johannes Pille, TheDeadMedic
If this question can be reworded to fit the rules in the help center, please edit the question.

If it helps, at the moment I am able to do what I want using this methodology. I'm not sure if it's the best way and I have not tested it outside my development environment. It seems to work so far. I'm hoping this inspires some feedback and possibly other solutions.

The basic idea is this:

1) Disable the built in calls to ShareThis that insert the buttons automatically on pages and posts (they do this with a call to add_filter('the_content', 'st_add_widget'); around line 285 in the sharethis.php file in the plugin).

2) create my own action that can be called to insert the ShareThis widget where I want in my template.

3) Do all this in functions.php so I don't have to hack the plug-in or Wordpress core. Doing it this way should allow WP and plug-in updates to run without any future issues.

Solution Added this to my functions.php

//remove share this from all content
function remove_sharethis() {
  remove_filter('the_content', 'st_add_widget');
  remove_filter('the_excerpt', 'st_add_widget');
  remove_action('wp_head', 'st_widget_head');
add_action( 'template_redirect', 'remove_sharethis' );

function print_sharethis_widget( ){
    print st_widget_head();
    print st_add_widget('');
add_action( 'custom_sharethis_widget','print_sharethis_widget');

Then in my templates I call the custom_sharethis_widget action where I want the ShareThis buttons to appear. like this:

<div id="my-sharethis"><?php do_action( 'custom_sharethis_widget' ); ?></div>

It took me a while to work this out, so hopefully this helps someone else out.

share|improve this answer
Appreciate your work on this but it involves modifying the plugins code. – Brad Dalton Feb 8 '14 at 9:33
@BradDalton Technically this changes the plug-ins behavior, but it doesn't modify their code. I'm using a call to remove_filter() in my theme's function.php file. I do not recommend editing a plug-ins code directly and I'm not doing that with this solution. – Adam Christianson Feb 11 '14 at 17:56

Note: The sharethis plugin has NOT been coded correctly using filters and may display the buttons in the sidebar therefore you may consider installing the code manually.

With the plugin installed

You'll need to use all 3 code snippets

What you need to do firstly, is remove the buttons which you can do using this code in your child themes functions.php file.

function remove_share_this() {
remove_filter( 'the_content', 'st_add_link' );
remove_filter( 'the_excerpt', 'st_add_widget' );
remove_filter('the_content', 'st_add_widget');
remove_filter( 'the_excerpt', 'st_add_link' );
add_action( 'loop_start', 'remove_share_this' );

This method enables you to display the buttons without installing the plugin.

Only need these 2 steps

Then you need to add the script to the pages you want to display the buttons on.

<script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js"></script>
<script type="text/javascript">stLight.options({publisher: "your-pub-i.d", doNotHash: false, doNotCopy: false, hashAddressBar: false});</script>

I used a themes script box however you could load this conditionally and hook it in using wp_enqueue_scripts

Then you add the span tags to your page which you can do using WordPress or theme specific hooks, otherwise directly in your template like single.php

<span class='st_sharethis_large' displayText='ShareThis'></span>
<span class='st_facebook_large' displayText='Facebook'></span>
<span class='st_twitter_large' displayText='Tweet'></span>
<span class='st_linkedin_large' displayText='LinkedIn'></span>
<span class='st_pinterest_large' displayText='Pinterest'></span>
<span class='st_email_large' displayText='Email'></span>
share|improve this answer
In the With the plugin installed you propose would I still use the call to my custom print_sharethis_widget() method to handle display in my template? Also, what's the advantage of using the loop_start hook vs. the template_redirect hook for the remove_share_this call? – Adam Christianson Feb 11 '14 at 18:07
I wouldn't use the plugin because i think Addthis is better however i think adding the buttons manually in any hook location without the plugin is a better option. – Brad Dalton Feb 11 '14 at 18:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.