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);

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");
$objects as $object ) {
get_the_tags($object->ID) != false) continue;
$content $object->post_content' ' $object->post_title' ' $object->post_excerpt;
$content trim($content);
$tags_to_add get_tagsFromTagTheNet($content))
wp_set_object_terms$object->ID$tags_to_add'post_tag'true );
$o['ID'] = $object->ID;      

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 (490)

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

fourteen + = twenty two