Converting WordPress Collation to UTF8Monday, July 13th, 2009 with 1 Comment »

For some foolish reason, the recent upgrades to WordPress 2.8 and 2.7 changes the collation of the database tables.  This results in the removal of special characters like Japanese, Chinese, or Korean.  All your characters would be lost if you upgraded because the collation is  latin1_swedish_ci instead of utf8_general_ci.  You’re lucky if you hadn’t used those characters before, but if you are planning to in the future, you can update your collation to UTF8 by running the following SQL queries in PHPMyAdmin:

ALTER TABLE wp_comments DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_links DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_options DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_postmeta DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_posts DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_terms DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_term_relationships DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_term_taxonomy DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_usermeta DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE wp_users DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Deleting All Posts and Comments from a WordPress BlogSaturday, July 11th, 2009 with 2 Comments »

The best way to understand the tables is to look at the WordPress Databae Design page, so you are aware of the dependencies.  I wanted a nearly clean copy of WordPress, so I executed the following SQL statements:

delete from wp_comments;
delete from wp_links;
delete from wp_posts;
delete from wp_postmeta;
update wp_term_taxonomy set count = 0 where 1 = 1;
delete from wp_term_relationships;

This deletes all comments, links, posts, post metadata, resets the count for the tags/categories/links to 0, and deletes the relationships.  I had to delete more than comments and posts, otherwise the tables would be in an inconsistent state.  For example, it may end up showing more than 1 post under a certain tag when all posts have been removed.   Thus, that explains why I had to set count to 0 for wp_term_taxonomy and remove all the links.

Updating All Internal Links in a WordPress BlogSaturday, July 11th, 2009 with 9 Comments »

Suppose you are switching domains and you want all links to point to your new blog.  There are 3 places where you need modifications: the author comment URL, the comment content, and the post content.  Comments are important as people may refer to your blog in the comments section, so it needs to be updated.

Perform the following queries:

UPDATE wp_comments SET comment_author_url = replace(comment_author_url, 'old URL','new URL')
UPDATE wp_comments SET comment_content = replace(comment_content, 'old URL','new URL');
UPDATE wp_posts SET post_content = replace(post_content, 'old URL', 'new URL');

Reference: How to Move WordPress Blog to New Domain or Location

Importing Large WordPress WXR FilesSaturday, July 11th, 2009 with 1 Comment »

I had to help someone import a 15MB WordPress WXR file today which failed because it was much too big (getting Fatal Errors). I ended up splitting the WRX as described by the article Import Large WordPress WXR File.

Each WRX needs 3 parts t o comply with the schema:

Header:
<rss version=”2.0?
xmlns:content=”http://purl.org/rss/1.0/modules/content/”
xmlns:wfw=”http://wellformedweb.org/CommentAPI/”
xmlns:dc=”http://purl.org/dc/elements/1.1/”
xmlns:wp=”http://wordpress.org/export/1.0/”>
<channel>
including all info like category, tags, etc to just before the first <item>

Body:
Your <item> tags

Footer:
</channel>
</rss>

I had accidentally saved the file without UTF8 encoding and that messed up the import badly. You need to make sure it is UTF8 if you have special characters.

Modifying the WordPress Comments TemplateSaturday, July 11th, 2009 with No Comments »

Reference: WordPress Codex

“This tag includes the file comments.php from your current theme’s directory. If that file is not found, it will instead include wp-content/themes/default/comments.php.”

You can copy the default comments.php and add modifications and place it in your theme’s directory. It is a good template to start with.

Fixing the RSS Widget Problem in WordPressThursday, May 14th, 2009 with No Comments »

Recently, certain feeds no longer worked for me on WordPress. I suspected my host had done something because these errors only occurred for WordPress installations hosted by them (the feeds worked on WordPress.com hosted blogs).

Errors for this problem can result in the text:
Warning: Attempt to assign property of non-object in /home/XXXX/public_html/XXXX/wp-includes/rss.php on line 449
Or
Error: could not find an RSS or ATOM feed at that URL.

Read more

Aligning Images in The Morning After WordPress ThemeWednesday, May 13th, 2009 with No Comments »

One of my favourite grid-based WordPress themes is The Morning After. However, the CSS can be messy to deal with because the theme uses an external CSS file in lib/compressed.css.

In lib/compressed.css, remove the code:

p{margin:0 0 1.5em 0;text-align:justify;}
p.last{margin-bottom:0;}
p img{float:left;margin:1.5em 1.5em 1.5em 0;padding:0;}
p img.top{margin-top:0;}

In stylesheet.css use the following code for alignment:

img.alignright {float:right; margin:0 0 1em 1em}
img.alignleft {float:left; margin:0 1em 1em 0}
img.aligncenter {display: block; margin-left: auto;
margin-right: auto; border: 0px;}
a img.alignright {float:right; margin:0 0 1em 1em; border : 0}
a img.alignleft {float:left; margin:0 1em 1em 0; border : 0}
a img.aligncenter {display: block; margin-left: auto;
margin-right: auto; border: 0px;}

Bulk Image Upload and Insert in WordPressSaturday, February 21st, 2009 with No Comments »

Occasionally I’ll need to upload and insert more than 100 photos into a single WordPress post. I haven’t seen any good bulk image upload and insert plugins, so it has been a gruesome task. However, with the use of Perl, BIMP Lite, and FileZilla the annoying aspects of this task have been automated.

Requirements
1. BIMP Lite - a free tool for Windows which I found from Smashing Magazine’s 15 Useful Batch Image Processors
2. An installation of Perl on your computer
3. FileZilla, or any FTP client
4. The Image-Size Perl Module, created by Randy R Jay, installed on your computer

The Basic Idea
The idea is to have a folder with all the images you need to upload. First, you use BIMP Lite to generate the thumbnails for you into a subdirectory called “thumb.” Next, upload all these images to your WordPress image uploads directory using your FTP client. Finally, use the Perl script below to generate the WordPress HTML.

Generator.pl

# Generates the HTML for bulk image upload and insertion for WordPress
#
# @author Cody (codelol.com)
# @date February 21, 2009

use Image::Size;

# The directory where the images are stored
$image_dir = ".";

# The directory relative to $image_dir where the thumbnails are stored
$thumb_dir = '/thumb/';

# The WordPress image uploads directory
# e.g. http://example.com/wp-contents/uploads/2009/02/
$wp_image_dir = "http://example.com/wp-contents/uploads/2009/02/";

# Currently only uses JPEG.  Update the regular expression as needed
opendir(DIR, $image_dir);
@files = grep(/\.jpg$/,readdir(DIR));
@files = sort(@files);

opendir(DIR, $image_dir.$thumb_dir);
@files_thumb = grep(/\.jpg$/,readdir(DIR));
@files_thumb = sort(@files_thumb);

$output = "";

$size = @files;
$size_thumb = @files_thumb;

# A very BASIC check to ensure there is the correct number of thumbnails
if($size != $size_thumb)
{
  # If the size does not match, exit
  die "The number of thumbnails (".$size_thumb.
  ") does not match the number of images. (".$size.")\n";
}

# Generate the WordPress code
for($i=0;$i<$size;$i++)
{
  # Determine the dimensions of the thumbnail
  ($width, $height) = imgsize($image_dir.$thumb_dir.$files_thumb[$i]);

  # Escape a single space by using HTML URL encoding
  # You may need to escape additional characters if necessary
  $file_escaped = $files[$i];
  $file_escaped =~ s/ /%20/g;
  $files_thumb[$i] =~ s/ /%20/g;

  $output .= '<a href="'.$wp_image_dir.$file_escaped.'">';
  $output .= '<img src="'.$wp_image_dir.$files_thumb[$i].'" ';
  $output .= 'alt="'.$files[$i].'" title="'.$files[$i].'" ';

  $output .= 'width="'.$width.'" height="'.$height.'" ';

  # You may need to edit the class attribute if you aren't using
  # algincenter or size-medium
  $output .= 'class="aligncenter size-medium" /></a>';
}
print $output;

print "WordPress code generated successfully!\n";
open(FILE, ">generate.txt") or die("Error");
print FILE $output;

Instead of manually uploading and inserting each image, these steps will basically automate most of these tasks…so you can go drink a beer and watch TV while waiting for the images to be uploaded by FTP.

Instructions
1. Make sure you meet the requirements
2. Run BIMP Lite to create thumbnails in a subdirectory in your images directory
3. Upload the original images and thumbnails to your WordPress images upload directory
4. Copy the Generator.pl into your images directory
5. Update $wp_image_dir in Generator.pl to your image upload directory
5. Execute the Perl file by double clicking on it
6. The generated HTML will be put into a new file called generate.txt
7. Open generate.txt and copy this code into your WordPress post

Widgeting Themes with Multiple SidebarsSaturday, January 31st, 2009 with 1 Comment »

Unfortunately, this theme (BloggingPro) was not widget enabled, so some work needs to be done. The Widgetizing Themes tutorial was useful for setting one sidebar, but in my case, there are multiple sidebars. Here is some code to make multiple sidebars widget ready…

First, this theme does not have a functions.php file for theme specific functions. Therefore, I uploaded wp-content/themes/[theme name]/functions.php. The file contains the following:

function.php

<?php
if ( function_exists('register_sidebar') ) {
  register_sidebar( array(
  'name' => 'sidebar_left',
  'before_widget' => '',
  'after_widget' => '',
  'before_title' => '<h2>',
  'after_title' => '</h2>') );
  register_sidebar( array(
  'name' => 'sidebar_right',
  'before_widget' => '',
  'after_widget' => '',
  'before_title' => '<h2>',
  'after_title' => '</h2>') );
}
?>

Next, we need to enable dynamic sidebars. Open wp-content/themes/[theme name]/sidebar.php.

We want to add dynamic sidebars for the div classes SRL and SRR. Different themes will use different names for sidebars div class names, so it may not look the exact same, but the concept is the same. After
<div class="SRL"> add:

 <?php if ( !function_exists('dynamic_sidebar')
        || !dynamic_sidebar('sidebar_left') ) : ?>

Right before the ending <div&gt for <div class="SRL"> add:

<?php endif; ?>

Similarly, do the same for the right side bar. After
<div class="SRR"> add:

 <?php if ( !function_exists('dynamic_sidebar')
        || !dynamic_sidebar('sidebar_right') ) : ?>

Right before the ending <div&gt for <div class="SRR"> add:

<?php endif; ?>

Now your theme is widgetized!

Get updates as often as we post! Subscribe to our full feed or comments feed.