WordPress 2.1 – Smilies!

Like a lot of WordPress users, I use the nifty WP-Grins plugin to make it easy to insert smilies like these :wave: :eek2: into my posts. It adds all my smilies in a row above the box where I type my posts, so I can click on them rather than remembering to type the code that makes them appear.

It was written for a much older version of WordPress, but with a minor change, it still works in much the same way for WordPress 2.0.x. There are excellent instructions here. But when I looked at WordPress 2.1, everything had changed. This morning, I had a search through the files, looking for the right place to insert the wp_grins() line, and after a fair bit of head scratching, I found it. Here’s what I did[1]

To get your clickable smilies into the edit page, you need to edit wp-includes/general-template.php. As of the latest RC code, look around line 825 for:

<script type="text/javascript">edToolbar()</script>

On a new line below this, add:

<?php if (function_exists('wp_grins')) wp_grins(); ?>

Save and upload the file, making sure you kept a copy of the unedited version.

That will get the row of smilies working, but you might find that smilies don’t always appear in your posts. This is because of some changes to how the code works. For technical reasons of some kind, there was a need to try to avoid characters being interpreted as smilies when that wasn’t what was intended. Unfortunately, for me, at least, this stopped a lot of intended smilies not appearing. The way to be sure that a smiley will appear is to make sure that the code you use has spaces around it. So, I made a change to the wp-grins.php file to ensure that any new smilies would have spaces around them, and so would definitely appear as images. Here’s what to do:

Edit wp-grins.php, so that it pads the smiley codes with spaces. Find line 34 and change it to this:

$grins .= '<img src="'.get_settings('siteurl').'/wp-includes/images/smilies/' .$grin.'" alt="'.$tag.'" onclick="grin(' '.$tag.' ');"/> ';

The only change (other than the same change of path needed for WP 2.0) is that this will insert spaces before and after the value of $tag.

Well, that fixes things for new posts. But what if you have hundreds or even thousands of old posts, which might have missing smilies? You might decide that you don’t really care, and let it go, or you might be like me and want to fix them all. But editing that many posts would take ages, and you’d be sure to miss a few.

What you need is a way to do a global replacement in your posts and comments. People who speak MySQL can probably do this in 0.005 seconds from phpMyAdmin, but for the code-challenged among us, something easier is needed. And fortunately, it exists! Enter the Search and replace plugin, which would have been useful to me previously if I’d found it at the time. It’s very simple to use. In one box enter the original text, and in the other the text you want to replace it with. Click the button and it very quickly runs through your posts and comments making the changes. As with anything that directly modifies the database, make sure you have a backup , and don’t blame me if it eats all your posts.

I found my list of smilies – depending on your setup, this will either be in wp-includes/vars.php, or if you have a lot of custom smilies, you may have done it the sensible way and put them in your wp-config.php file. Either way, you’ll have a list that looks like this, only longer:

‘;–)’ =>’icon_wink.gif’,
‘::–8:’ =>’icon_cool.gif’,
‘:hi:’ =>’icon_wave.gif’,
‘:agrin:’ => ‘icon_cheesy.gif’,

Your own values will vary. All you have to do is replace every ;–) with ;–) [2]. And so on for every smiley. It won’t take too long, and it should make all your smilies work. Have fun. :wave:

[1] I posted the same thing on the WordPress support forums
[2] That’s a space before and a space after the ;–)[3]
[3] I’m using a code that’s not in my list to stop it being smilified [4]
[4] I hope :rolleyes: