Sep
15
Posted on 15-09-2006
Filed Under (Plugins ) by jay

A few days ago noticed that the Tag Suggestion functions in Ultimate Tag Warrior were no longer working. So I spent a few hours this week looking into the issue and came up with an alternative solution. UTW provides two Tag Suggestion options. One is from Yahoo and the other from Tagyu. The Yahoo service stopped working because it requires an AppID which I can only assume has either expired or has exceded is maximum number of requests and the Tagyu service stopped working because it has been shut down by the owner.

Well I really liked the Tag Suggestion service so I decided to either fix it in UTW or find an alternative. I fixed the Tagyu service by replacing it with a similar service from tagthe.net and this is what this document is about. But the fix for I the Yahoo service requires an AppID so by hacking the code (similar to the code below) and using my own Yahoo AppID I got the service running again.

Before I explain anything you must understand and comprehend that this fix will alter the underlying source code to Ultimate Tag Warrior.
What does that mean?
It means that if and when the next update to UTW comes out it it will not have these changes and upgrading will most likely over-write these changes.

This hack will replace the Tagyu tagging service with the tagthe.net tagging service. tagthe.net works by sending the contents of a document to their service and they send back a list of tags just like the Yahoo service.

You'll need to install the WP-Scriptaculous plugin to make use of Prototype's Ajax functions.

The files we are going to edit:
ultimate-tag-warrior-ajax.php
ultimate-tag-warrior-ajax-js.php
ultimate-tag-warrior-actions.php


Before you begin make bakup copies of these files.


Add the Service Code

Open up ultimate-tag-warrior-ajax.php and scroll down to about line #65 and look for the switch ($service) statment and add this new case:

PHP:
  1. case "tagthenet":
  2. /*
  3. Send a HTTP GET or POST request to http://tagthe.net/api/?[METHOD]=[VALUE].
  4. You will receive a response document with the tags we found in the text.
  5. Text * Example: http://tagthe.net/api/?text=Hello%20World!
  6. */
  7. $keywordAPISite = "tagthe.net";
  8. $keywordAPIUrl = "/api/?text=";
  9. $pattern = "/(<item>)(.*?)<\/item>/i"; //.*<//tag>)/i";
  10.  
  11. $noUnicode = preg_replace("/%u[0-9A-F]{4}/i","",$content);
  12.  
  13. $data = urlencode(strip_tags(urldecode($noUnicode)));
  14.  
  15. $data = str_replace('%2F','/',$data);
  16. $data = str_replace('%09', '', $data);
  17. $data = str_replace('%26%238217%3B','\'',$data);
  18. $data = str_replace('%26%238220%3B','"',$data);
  19. $data = str_replace('%26%238221%3B','"',$data);
  20. $data = str_replace('%26%23038%3B','%26',$data);
  21.  
  22. $curl_url = 'http://' . $keywordAPISite . $keywordAPIUrl . $data ;
  23.            
  24. break;

This code works just like the Yahoo and Tagyu services code.

Add the Ajax Code
Open up ultimate-tag-warrior-ajax-js.php. This is where we are going to create some hooks for the UI to send text and get back some tags. At the bottom of the document add this:

JavaScript:
  1. //tagthe.net
  2.     var handlerTagTheNet = function askTagThenetForKeywords_Callback(str) {             
  3.            document.getElementById('tagthenetSuggestedTags').innerHTML = str.responseText;         
  4.       }
  5.      
  6.     function askTagThenetForKeywords()
  7.     {
  8.         document.getElementById('tagthenetSuggestedTags').innerHTML = 'Loading...';
  9.         var content = document.getElementById('content').value;
  10.         new Ajax.Updater('tagthenetSuggestedTags','<?php echo $ajaxurl ?>?action=requestKeywords&service=tagthenet&content='+content+'', { method:'get', asynchronous:true,  onSuccess:handlerTagTheNet });           
  11.     }

This code is different from the other ajax code on this page because instead of using a XMLHttpRequest we are useing the Ajax functions from the Prototype library. You'll also see that there is a new elementid we are referenceing tagthenetSuggestedTags which we will be adding in the next section.

Add the UI Code
This last section is where we are going to hook up an onClick eevnt to the Ajax function and provide a place for the Ajax function to send the tag suggestions. Open up ultimate-tag-warrior-actions.php.
I will reference each line ny line number but this may differ slightly by a few lines for your code due to other changes I may have made that altered the number of lines in the document.

Line #601
Change the button onClick event from asktagyuForKeywords() to askTagThenetForKeywords().

PHP:
  1. $suggestions .='<input type="button" onClick="askTagThenetForKeywords()" value="Get Tags From tagthe.net"/>';

Line #603
Change the div id where the tag suggestions will be displayed to match the one form the ajax function

PHP:
  1. $suggestions .='<div id="tagthenetSuggestedTags"></div>

Line #606
Change the link back to tagthe.net.

PHP:
  1. echo '<fieldset id="tagsdiv" class="dbx-box">' . '<h3 class="dbx-handle">Tag Suggestions (Courtesy of <a href="http://tagthe.net">tagthe.net</a>/<a href="http://www.yahoo.com">Yahoo!</a>)</h3><div class="dbx-content">' . $suggestions . '</div></fieldset>';

Finishing
Save everything and upload it to your server. Open up the admin Manage->Posts select an existing post to edit. Scroll down to the tag Tag Suggestions and you should see your new tagthe.net button. Click it and some text below it should disply a Loading... message then a few seconds later some links should appear. Here are your new tag suggestions. This is a fairly simple hack but like I said it does alter the base code of UTW

If you are having any trouble you should use the Firefox extension: Firebug this can help you debug the HttpRequests as well as your Ajax functions.

    Read More   

Comments are closed.