Customize Plugin Order Below WordPress Posts Using Add_Filter

There are a number of wordpress plugins available today that would display some particular content at the bottom of your wordpress posts. These plugins could be anything ranging from subscription options for your blog, sharing links for the post OR a plugin to display related posts to the existing post. Once you enable the requisite plugins, you would see the various outputs appearing at the bottom of every post. As an example, for the post on call to action button, the following snapshot show the information displayed by the plugins just after the post ends.

Plugin Order Original

As you would notice above, the last paragraph of the post ends with <You can always submit any useful tools, web services and tips to us.>. Just below this text, there is output provided by the following three plugins --

While the plugins do their job, I was not happy with the order in which they appeared below the post content. Essentially I wanted the plugins to display in the following order.

The idea for this order was to club the plugins logically. To encourage the visitors to share the post, I wanted to have the sharing links immediately after the post content. This way the probability of a visitor using the links increases as opposed to having them after the Outbrain posts. Just after the sharing  options, I wanted to have the Outbrain related posts to provide the visitor with a nice visual feel. At the end, I wanted to have the related posts as provided by the Yet another related posts plugin.

I had not specified any order of appearance for these plugins as such. This was the default output that I got after installing all these plugins. Now I had to figure out how to modify this order without too many changes. A little knowledge about wordpress php helped in this regard. Basically either of the plugins uses the add_filter hook to add content at the bottom of wordpress post. The syntax is as given below.

 <?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?> 

The above syntax just says using the function <function_to_add>, update the content of <tag>. In our case, <tag> would contain the native post text content that has been written. The <function_to_add> function would specify what is to be added to the bottom of the post  and would return the current content( i.e the native post text) concatenated with the further content as desired by the plugin ( eg if we assume that first add_filter is done by Sexy Bookmarks Plugin, then it will be the various post sharing animated image icons ). This returned final content would be treated as the current content for the subsequent call of add_filter i.e the current content in the next add_filter invocation would would be the final content returned by the previous add_filter which in this case would be the native text+sexy bookmark icons and hence anything else would be displayed below the sexy bookmarks plugin output. Each of these plugins would use the above add_filter to append their content to the existing post.

So to solve the problem, I started to explore the add_filter options. This is where the <priority> option details led to the solution


 $priority
    (integer) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

Default: 10 

So the idea was to use this priority to make the  Sexy Bookmarks Plugin options appear first of all. For doing the same, I just searched for the add_filter inside the wp-content/sexybookmarks/ folder and found it to be available in wp-content/sexybookmarks/includes/public.php. I did a single line change as follows

Original Code

 add_filter('the_content', 'sexy_position_menu'); 

Final Code

 add_filter('the_content', 'sexy_position_menu', 0); 

As you would notice, I just specified a priority value of zero to make sure that the add_filter done by the Sexy Bookmarks Plugin is given priority and appears on top of the add_filter done by other plugins. With this simple change, the problem got resolved and the same snapshot appears as follows with the desired result.

Plugin Order Final

The only downside is that I did this change in the plugin file for Sexy Bookmarks Plugin. So if I upgrade or replace this plugin tomorrow, then this change will no longer have a utility. The positive part is that as long as one is clear about the change and semantics of it, this should work well for a good time. If you have multiple plugins and want to specify the order explicitly, you might need to specify the above change in multiple files i.e add_filter for each plugin.

Do try this change yourself if you found it useful and share your feedback. If you think there are better and easy way to achieve the same result, do update us in the comments section. You can always submit any useful tools, web services and tips to us.





Inficone runs on the Genesis Framework

Genesis Framework
Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!
Follow On Twitter
Follow On Facebook
Follow Using RSS
Follow Using Email
Tweet This Post
Share Post On Facebook
Digg This Post
Stumble Upon

Comments

  1. Great tips to Customize Plugin Order Below every WordPress blog Posts.
    Thanks,

  2. Thanks for the tip , it worked like a charm.

  3. Hey matey. This is just the info I needed, just wanted to say thanks a lot 🙂

  4. Shivam, you’re a star!

    I’ve been trying to change the order of my “Share This” plugin on my site and thanks to your instructions I’ve finally been successful.

    I’ve also been trying to find some way to append my posts with html much like your Genesis Framework box above. Do you just use a custom field for that or is it a plugin.

    Thanks again for your help! Much appreciated.

  5. This post help me. I also have more than one plugin appearing after the comments on the single post pages. I wanted to change the order in which they appeared. I applied your solution and it worked for me.

    Thank you for sharing this Shivam.

Speak Your Mind

*

This blog is kept spam free by WP-SpamFree.