How can I position ShareThis buttons manually when using the plug-in?

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.

Related posts

2 comments

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

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

Comments are closed.