Home > Code Snippets, PHP > Using TagTheNet to generate tags on WordPress

Using TagTheNet to generate tags on WordPress

August 15th, 2009 Leave a comment Go to comments

Recently on Kerala News by Asianet, though the wp-simple-tags was there, the posts were not being tagged automatically. And on a detailed check I found it was due to a misconfiguration, and once the same was done properly the tagging started smoothly. But already a set of 8K posts were there with no or unrelated tags mistakes and un awareness of the operators who were posting to the site. Now I wanted these to be tagged properly.
I had already used the word-twit plugin and done some mods to the same, so it was more eaiser for me. Just went through the original code of word-twit and salvaged a small script which is attached here with as download.
require_once( dirname(__FILE__) . '/wp-load.php' );
The code above loads the wordpress system and initializes the wp variables and connects to the database.
global $wpdb;
Makesure we have the database abstraction object from wordpress, to select the posts, and do all the required manipulations on the same.
require_once( ABSPATH 'wp-includes/class-snoopy.php' );
Include the inbuilt snoopy class, to mimic a web browser with the most simplicity.

function get_tagsFromTagTheNet$content ) {
   
$location 'http://tagthe.net/api/';
   
   
$snoopy = new Snoopy;
   
$snoopy->agent 'Saturn AutoTagger Bot 0.1; http://www.saturn.in';
 
   
$post_fields = array( 'text' => strip_tags($content), 'view' => 'json' );  
   
      
$result $snoopy->submit$location$post_fields );
      if ( 
$result ) {
         
$output $snoopy->results;  
 
         
$res = @json_decode($output);
         if(isset(
$res->memes[0]->dimensions->topic[0])) 
             return 
$res->memes[0]->dimensions->topic;
      }
 
    return 
false;
}
 

This is the core of my solution, we pass the content, and get an array of tags. Thanks to Tag The Net and their api service for developers. Inside the function, we instantiate the snoopy class, and set it such that it will post the content to tagthenet api and get the tags as a json.
$bookMarkFile $_ENV['HOME'].'/autotags.bm'
 
$o unserialize(file_get_contents($bookMarkFile));
$j $o['j'];
 
  
$objects = (array) $wpdb->get_results("SELECT p.ID, p.post_title, p.post_content FROM {$wpdb->posts} p WHERE post_type = 'post' ORDER BY ID DESC LIMIT {$j}, 30");
 
  foreach( 
$objects as $object ) {
   
$o['j']++;
   if(
get_the_tags($object->ID) != false) continue;
 
      
$content $object->post_content' ' $object->post_title' ' $object->post_excerpt;
      
$content trim($content);
      if(
$tags_to_add get_tagsFromTagTheNet($content))
    
wp_set_object_terms$object->ID$tags_to_add'post_tag'true );
 
      
$o['ID'] = $object->ID;      
  }
 
file_put_contents($bookMarkFileserialize($o));

Here I am trying to do a small subset of 30 posts in one session, to keep the load on the server at an average. Hence I keep a bookmark, in the serialized form, and adding the ‘ID’ to the bookmark, is just for me to check out periodically. And this file will be run using the system cron every 10 minutes from the php cli.
The whole code is attached as down loadable TagTheNet Wordpress autotags (501)

  1. No comments yet.
  1. No trackbacks yet.

sixty one + = sixty six