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.
2 comments
Comments are closed.
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 thesharethis.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
Then in my templates I call the
custom_sharethis_widget
action where I want the ShareThis buttons to appear. like this:It took me a while to work this out, so hopefully this helps someone else out.
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.
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.
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