WordPress – tag post appending plug-in…..

Found in categories: Development, PHP, Wordpress.

WordPress – tag post appending plug-in…..

Wordpress plug-ins
I’ve been using WordPress more and more recently as it’s an ideal platform to speed up development of sites with the additional benefit of it being a well respected CMS and allows the site owner to update content through the interface with no knowledge required of HTML. There’s an abundance of available themes and some great ones which are free with a one click install. Even better and more importantly it’s simple.

Anyway, I’ve always dabbled with WordPress and had an interest in it. There are alternatives in Drupal, Joomla and of course custom builds but WordPress is adequate for most requirements. These days I’m trying to get a better understanding of how the platform works in it’s life cycle as more jobs require some knowledge of it. I often use the available plug-ins with my install but haven’t really dabbled until now with their inner workings. Anyway, having got a fundamental understanding of them recently and realised how useful some plug-ins can be I decided to write a few very simple ones of my own, just to get me started.

One thing I have noticed about blogging is how useful tags are to a site and bloggers overall SEO efforts. I for one use them when I blog and I try to make them very pertinent to the content I’m covering. We don’t need to penalised by Google anymore than is necessary for what it considers black hat tactics and with recent and ongoing changes to the algorithm and not truly knowing the context of those changes, we have to box clever.

I always come back and reviewed posts at some point later to make sure my tags reflect my content, otherwise, my efforts would simply be in vain. Well recently I came across some simple code, using the API hook, for a plug-in to add tags to every post being finally published by the author. This is accomplished by tapping into the action hook in WordPress, post_publish. Now in my case over on my Android Scoop site, useful tags undoubtedly would be aside from post specific ones, Android, Scoop, android, scoop and maybe ANDROID, SCOOP, and even maybe, ANDROID SCOOP. They would be useful on all posts so Google hopefully indexes them, associated with my site. To allow for the bare minimum, I have created a plug-in that will automatically append the tags, android and scoop to every post, regardless and in addition to what that user tags the post as. That blog is a community blog so it’ll have several users all posting who may not tag their posts in a certain way. All posts originate from that blog, so this makes most sense.

The code for the plug-in is below and this plug-in simply sits in the plug-in folder of the install but requires activation on the back end through the plug-ins page.

Additionally, you’ll also notice the function in this snippet isn’t named, it’s anonymous, and is inside the call to the action hook, so this should avoid any conflicts or collisions over duplicating a function name within WordPress. This could also have been achieved using a class and method within that.

//code to add android, scoop to a published blog post.

//The action hook we'll tap into is 'publish_post'>, when a user publishes a post pressing the publish button.
add_action("publish_post", function(){
//Get the post id.
$post_id = get_the_ID();
//Tags you want to append after the post, as an array.
$tags = array("android", "scoop");
//Post tags to published post
wp_set_post_tags($post_id, $tags, true);

And that’s it!. Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *