December 6th, 2009

Tutorial: Autosave WordPress Post Title as Tags

This post is published in response to a WordPress support thread, requesting such a functionality (http://wordpress.org/support/topic/338629)

Although the title of the post states that it is a tutorial, I haven’t really tested the following code extensively. It works on my test environment as well as a live blog which I tested it with.

Here’s the initial code:


function SaveTitleAsTag($post_ID) {
 $gpt = get_post($post_ID);
 $posttitle = $gpt->post_title;
 $posttitle = strtolower($posttitle);
 if(get_the_tags($post_ID)){
    foreach(get_the_tags($post_ID) as $tag) {
      $tag_name = $tag->name;
      $tag_nameĀ  = strtolower($tag_name);
      $posttitle = str_replace($tag_name, "", $posttitle);
    }
 }
 $splittotags = explode(" ", $posttitle);
 foreach ($splittotags as $atag){
     $atag = str_replace(" ", "", $atag);
     if($atag !=NULL){
           wp_set_object_terms($post_ID, $atag, 'post_tag', true );
     }
  }
}

add_action('save_post', 'SaveTitleAsTag');

I’ve wrapped the above code up in a plugin. Just download the file, upload to your WordPress plugin folder and activate.

Download

NOTE: For the latest version of the plugin and updates to the code, please visit the download details page: http://fusedthought.com/downloads/autosave-post-title-as-tags-wordpress-plugin

Shortlinks

2 Comments

Subscribe to Comments
  1. Richard

    Many thanks for this creative idea. I’ll check this out locally and see how it works!

Leave a Comment

Or Comment using....

Facebook Connect

Skip the hassle of typing in the required fields! Link Fusedthought Studio with your Google, Facebook or other connect enabled accounts.

Top