Highlight the Author’s Comments

6th of June, 2008

I always neglect my personal site when working on other consuming side-projects.  Lately I've been hanging out at the wordpress support forums, because my latest project involves wordpress.  When I go there to search for answers, I invariably spend a minute and answer a couple of recent threads.

The mood of my reply to those threads varies with the quality of the answer I've just found for myself.  Not intentionally of course, it's just one of those cosmic-balance things :)

This is another in a burst of wordpress posts for me lately, partly because I'm hacking around with it, but also because a few of my most recent visitors have come from the wordpress forums via posts I've made there.

Christine asked me about making her own comments stand out from everyone else's.  This is one of the most widely-embraced features of all, I think, and with good reason.  It really makes it easy to find the official word amongst all the chatter (not that I have a big problem with excessive chatter here, mind you).

I directed her to a plugin called Highlight Author Comments, but plugins aren't really necessary for this sort of thing, if you're comforable with editing your theme.

The basic idea is to take the main comment element in your design, and give it a different CSS class.  This allows you to differentiate between one type of comment and another.  It's quite easy, if you can find where your particular theme puts this information in the first place, as all themes are different, this part is up to you, unfortunately.

Your theme's comment.php will is the place to look.  You might find either a div tag, or a list-item tag with the style of your comments applied to it, like this perhaps:

<li class="comment">

Our aim is to change that for the comments made by the post author, so it looks like the following, but only for that specific comment:

<li class="author_comment">

To do that, we'll need to put a little wordpress php magic in place of the 'class' attribute.

<?php
 if($comment->comment_author_email == get_the_author_email())
   $comment_class = 'author_comment';
 else $comment_class = 'comment';
?>
<li class="<?php echo $comment_class; ?>">

Now in your CSS file, just take the existing .comment definition and make a new one called .author_comment, which you can then change accordingly.

.comment {
    border: 1px solid blue;
    background-color: #88ccff;
    }
.author_comment {
    border: 3px solid red;
    background-color: #ffcccc;
    }

Of course yours will be a bit more complex, but that's the general idea.  Given the differences in themes, it's hard to be overly specific so something like this is best suited to those who are already up to their elbows in theme customisation.  For the rest, there's always that plugin I mentioned above.

You can get as creative as you like really, and it works on multi-author blogs just as well as it will on your personal blog, bearing in mind that if you're not the author of a post, your comments will not appear as though they're special, even though you might be the owner of the site and a total rock star.

Trust me, you'd have been bored if I didn't pad this post out with some back-story.

Busy Playing Games

27th of May, 2008

For the past couple of days I've been involved in setting up a surveillance system at a client's premises.  It's a system of wireless cameras each streaming video to a central repository.

The nice thing about it is that anything with a web browser can pull the camera feeds, so they'll end up using their mobile phones when away from the office.  When they asked how they could get the feeds up onto the big-screen TV, I said they could even use a games console with a web-browser if they wanted.

When I turn up at their office I see they've bought themselves a Wii in anticipation of viewing their security footage through it.  So instead of doing any work, we bowled for most of the afternoon.

So much for it being a legitimate business tool. :)

Turn a Wordpress Page into a Link

20th of May, 2008

I've been working with wordpress a lot lately though there's no real evidence of it on this site, unfortunately.  Nevertheless it's resulted in a few little 'tweaks' which I think may warrant their own little section here.  I'll think about that as keep collecting them.

In the mean time, I want to post something which I'm not using personally, but which provides a quick and creative (if a little dirty) solution to a theme-based problem that almost everyone who uses wordpress grapples with at least once.

A lot of the prefab themes out there use page-based navigation menus, but unfortunately this system doesn't allow you to insert custom links wherever you want them.  So what's the solution?

Well, using this little page template, you can turn any page into a link that leads anywhere you want it to.  Being careful to avoid any leading white-space, save the following code as 'redirect.php' and upload it to your theme's folder.

<?php
/*
Template Name: Redirect
*/

if (have_posts()) {
  the_post();
  $pattern = '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@';
  preg_match($pattern, get_the_excerpt(), $matches);
  if ($matches[0]) header('Location: '.$matches[0]);
  else echo 'Enter a URL into your page body text.';
}
?>

Now when you create a page, you'll see the option to use a template called 'Redirect'.  When using that template, your page can have any title you like, but the page content should be nothing but a link to your chosen destination.

Using wordpress' built-in page ordering mechanism, you should now be able to position that linky-page anywhere you feel it fits in your navigation menu.

This is one of the most common problems people have on the wordpress support forums, and it turns out the solution is actually quite achievable even for people who aren't confident enough to make changes to their theme's PHP templates.

There are of course better ways to build your menus, but if you're otherwise happy with your theme and all you want is a link or two, then here it is.

10,000 cans of lunch meat on the wall…

19th of May, 2008

This site used to bear the tagline "a website you don't visit, by a man you don't know".

That's as true today as it was back then, but unfortunately it doesn't apply to spammers.  I've just broken 10,000 spam messages since the re-launch of this site on the 31st of August 2007.  That means that less than 5% of my comments are genuine people.

... and that's in spite of all I've done to reduce the incidence of spam ever being posted in the first place, like removing the more obvious honeypots and IP-blocking repeat offenders.

For the most part, the internet is a horrible place and we're all just doing our best to keep from sinking into the cesspool (except on Saturdays when I get all dressed up and dive into it).

Themed Multiple Domains in Wordpress

16th of May, 2008

Traditionally, wordpress does not allow you to use multiple domains on your site.  It will always redirect them to the URL you have specified in your options.  Generally speaking, that's exactly the right way to go about it unless you have an advanced purpose in mind.

Why would you want this?

Well, if you have multiple domains relating to your site (including subdomains) you can use this method to serve alternative content on those domains, from a single wordpress installation.  You can even use an entirely different theme for each domain, if you like.

Why did ∗I∗ want this?

Well, I am working on a project which needs to be accessible from a mobile phone or PDA.  A method using browser-detection is unreliable, simply switching out style sheets may result in elements and images being downloaded even if they're not displayed -- so I decided to create an entirely new (much lighter) wordpress theme and serve it from a .mobi domain.  After all, that's what .mobi domains are for, right?

Surely this is 'out there' already...

Well, yes, sort of.  When poking around for a ready-made solution I found a guy who thanked another guy for making a plugin to do this.  It's kind of old and not particularly well supported, so I wasn't really overjoyed at the thought of making it an essential part of my new project's future.

The other thing that bothered me is that the plugin was really quite active.  It filters all the generated links on your site, and sifts through all your content looking for absolute links to edit to match your additional domain.  This is probably exactly what you want if you have heaps of posts with hard links in them by the time you decide you want a new domain added, but I don't need that.

If that is what you want, there's a more up-to-date and quite nice-looking plugin available from this guy.

Why I didn't want a plugin

It's not that I don't like plugins, it's just that this is going to be core functionality for my new project, so I really don't want to have to wait for the plugin to be updated if there are major changes to the wordpress platform (quite common).  I want something simple, and I'm prepared to use relative links in my posts from day one.

How to do it

It's actually surprisingly simple.  You just need to modify your wp-config.php to set a few constants.

In wp-config.php find:

$table_prefix = 'wp_';

Underneath that, insert the new code:

function domain_theme($domain) {
   // edit this path to match your server
   $themepath = '/path/to/public_html/wp-content/themes/';
   define('WP_SITEURL', 'http://'.$domain);
   define('WP_HOME', 'http://'.$domain);
   define('TEMPLATEPATH', $themepath.$domain);
   define('STYLESHEETPATH', $themepath.$domain);
}
$domain = $_SERVER['SERVER_NAME'];
$domain = str_replace('www.', '', $domain);
// use one or more additional domains here
if ($domain == 'altsite.mobi' ||
    $domain == 'anothersite.net' ||
    $domain == 'thirdsite.com'
   ) domain_theme($domain);

You don't need to put your default domain in the last if-statement, this is just to verify that the domains you want to be themed are in fact set with their own theme folder which is named after the domain itself.

if you have lots and lots of domains with their own themes, you can change the last if-statement to read:

if ($domain !== 'defaultsite.com') domain_theme($domain);

That will result in errors unless you have a theme for each domain already uploaded.

If you want to allow multiple domains, but want to use the same theme for each domain, you can simply remove the lines containing TEMPLATEPATH and STYLESHEETPATH.

Drawbacks

If you don't have substantially different content on each of your domains Google will penalise you heavily.  You can avoid this by using a robots.txt file for your additional domains.  This is ideal for cases like mine, where you're only serving mobile content from the additional domain.

Preventing the Indexing of Duplicate Content

UPDATE: I've been asked to provide an example of how to block duplicate content using robots.txt files, so here's how I'm doing it:

I'm using two distinct robots.txt files.  One with the regular name, and another called robots_blocked.txt.  I then differentiate between them using .htaccess and mod_rewrite, information about which is infinitely googlable.

Here's what I have in my .htaccess file, directly above the wordpress section which should already be in there if you're using 'pretty' permalinks:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?yourmainsite\.com$ [NC]
RewriteRule ^robots.txt$ /robots_blocked.txt [NC,L]
</IfModule>

This basically ignores any requests for robots.txt via your main site, instead passing them to the real robots.txt file as they should be.  If you try to access robots.txt via any other domain (or subdomain) the rule is activated, and you're served robots_blocked.txt instead.

For the sake of the internet and your ranking in search engines, please ensure that any duplicate content (that is, the vast majority of your site) is properly excluded via your robots_blocked.txt

Close
E-mail It