DIY WordPress HACK – How to import Ultimate Tag Warrior Tags if you keep getting errors!

Well, since I’ve upgraded to WordPress 2.5, I’ve had to let go of Ultimate Tag Warrior. Actually, I thought Ultimate Tag Warrior was working until today, when I found out every URL such as http://zedomax.com/tag/lasers, was giving me a 404 error.

Well, being an old school I am, I did even try to downgrade back to my favorite WordPress 2.2.3, which results in catastrophic failure with the wp-categories… Damn it!

So I did look through the internet and basically WordPress 2.5 has a built-in function to let you import Ultimate Tag Warrior tags.

Even though it should have gone smoothly, I did encounter LOTS of problems as the built-in function was very basic and did not support importing of 47,000 tags, which is what this site has.

The first problem I had was that I keep getting the following message:

Done! 13331 tags were read.
Reading UTW Post Tags…
Done! 45824 tags to post relationships were read.
Adding Tags to Posts…
Done! 0 tags were added!

Now, as you can see everything worked until the last line, “Done! 0 tags were added!”. I was so furious to know why it wasn’t working, so I even downgraded to WordPress 2.3.1-RC, which others were saying that it worked.

Now when I tried to import, I did get this error message:

WordPress database error: [Got a packet bigger than ‘max_allowed_packet’ bytes]

This one is easy, this simply means your MySQL server is not set to handle large amount of data.

So I did change the max_allowed_packet size to 1 gigabyte like this by add this line:

set-variable = max_allowed_packet=1024M

(Yes, 1 gig should do the job I thought…)

Well, it turns out that after this change, I don’t get the “0 tags were added” message, but the screen “hangs” at the last step of the import screen…

As a more of a good hacker than a developer I am, I finally decided it was time to hack the WordPress itself.

When I looked into the import php file, utw.php which is under wp-admin/import directory of your WordPress blog.

It turns out that the import program is very basic at most and the programmer didn’t even consider large chunks of data.

So instead of doing 1 MySQL query, I changed it so it only fetches 100 data bits at a time.

If you are having problems like me with a large tag database, you can change the following function in utw.php like this:

function tag2post ( ) {

// get the tags and posts we imported in the last 2 steps
$tags = get_option(‘utwimp_tags’);
$posts = get_option(‘utwimp_posts’);

global $wpdb;
$startvalue=0;
$endvalue=1000;
$ct=$startvalue;

$tags_added = 0;
while($ct<$endvalue) {

$posts_query = “SELECT tag_id, post_id FROM ” . $wpdb->prefix . “post2tag”.” LIMIT “.$ct.”,100″;
$posts = $wpdb->get_results($posts_query);

if($posts) {
// null out our results

// loop through each post and add its tags to the db
foreach ( $posts as $this_post ) {

$the_post = (int) $this_post->post_id;
$the_tag = (int) $this_post->tag_id;

// what’s the tag name for that id?
$the_tag = $tags[$the_tag];

// screw it, just try to add the tag
wp_add_post_tags($the_post, $the_tag);
$tags_added++;
echo “Num: “.$tags_added.” – “.$the_tag.”<br />”;

}

}
$ct=$ct+100;
sleep(0.1);
}
echo “HACKED by Max Lee from http://zedomax.com yey!”;

// that’s it, all posts should be linked to their tags properly, pending any errors we just spit out!
return $tags_added;

}

Download my UTW.php here (rename utw.txt as utw.php)

Now, my recommendation is to run the Step 3 of the UTW import by taking 2000 tags at a time. You can change the value of $startvalue and $endvalue to control the range of your imports.

FInally, I did get all my precious tags imported, now you can install the Tag Suggest Thing and some other cool stuff from the UTW.

Phew, that was a long 4 hours of hacking WordPress. Although I am very impressed at the new WordPress 2.5, functions like the Ultimate Tag Warrior import could be improved dramatically through use of smarter programming and AJAX.

What I do recommend that WordPress developers do is use AJAX to fetch at most 1000 tags at a time and then update the screen and the database to protect again freezing or users having to re-configure their MySQL server.

I also found out that if your PHP setttings for max_execution_time is low, (mine was at 10 seconds), this can be a problem as the import process takes longer than 10 seconds for big databases.

So change it like this in your /etc/php.ini file:

max_execution_time = 90 ; Maximum execution time of each script, in seconds
max_input_time = 95 ; Maximum amount of time each script may spend parsing request data
memory_limit = 128M

Then you can change it back after import.

Now, I’ve outlined all the possible problems you can have while importing your “large” number of tags.

For blogs with little or less than 2000 tags, the default WordPress importer shouldn’t be a problem but for people like me with over 10,000 tags, this is a big problem but I am sure WordPress developers will update it in the future. Check me out as I tried to get help from WordPress Forum, but I did figure it out eventually myself.

5 Responses to DIY WordPress HACK – How to import Ultimate Tag Warrior Tags if you keep getting errors!

  1. Chris from Detroit says:

    Genius! Thank You. I’ve been so afraid to upgrading because of this problem. I’d lose a lot of useful links to my website.

  2. Keith R says:

    I also had trouble trying to import my UTW tags once I upgraded to WordPress 2.5.1 from 2.1. I tried your hack, and it got me trhough the first couple of steps (I was having getting even that far before!), but I am hitting your same “zero tags” error at step 3:

    Done! 0 tags were added!

    I have 4642 tags and 5000 tag-to-post relationships. Any ideas?

    Thanks,
    Keith

  3. max says:

    Try changing the values, start with startvalue at 0 and endvalue 100.

    Depending on your server settings, the amount of tags you can import at a time can vary.

    For me I could do 5000 tags at a time. I had about 50,000 tags so…

    You might be okay doing 500 to 1000 tags at a time.

  4. John Bollwitt says:

    Tried this on a WP2.6.3 install. Your hack didn’t work, but then went back and tried the original importer and it took.

  5. max says:

    Well, this hack is if the original importer didn’t work plus I think I made it during WP 2.5 but glad it worked out for ya.

Leave a Reply

Your email address will not be published.


Check out more interesting categories: Misc.


Related News and Resources