Posts Tagged ‘plugin’

Smart Ads: small effort, potentially big ad revenue rewards

Wednesday, June 11th, 2008

I have seen a number of plugins that will allow you to manage every single detail of your Google AdSense ads and Smart Ads is not one of them. Its managing capabilities are minimalistic, perhaps even too much so for the novice user. However, it excels in a different area: smart ad placement. And that won me over.

In an ideal world the length of your posts is more or less the same and all of them are equally useful and related to the main subject of your website. In reality, well… you get my point. All the AdSense managers I have seen so far, despite their sheer wealth of options, do not take this into account. You can create the perfect ad or ads, but once it’s done it will be added to every post on your website, with few exceptions.

Smart features

Smart Ads allows you to:

  • specify a minimum post word count. Ads will not be shown on tiny posts, where they might take center stage and perhaps contain more text than the post itself. Not good!
  • show ads only on posts older than x days. Very useful to improve the relevancy of the ads shown, which in turn might lead to a higher click-thru-ratio.
  • exclude certain categories. Posts in these categories will not have any ads.
  • take into account images. If you intend to show the ad block inline and have your post wordwrap around it, then images can really mess up your layout since the don’t play nice with wordwrap. Smart Ads can look for images in the first x characters from the top and now show any ads if an image is found, preserving your layout.
  • disable ads for registered users. Imagine a subscription-based website, you can offer your subscribers an ad-free website.
  • decide on a per-post basis to disable ads.

Less user-friendly, more flexible

As mentioned earlier, a potential downside of Smart Ads is that you will have to do some of the work itself in getting it to actually display ads: no fancy manager. Here is an example from my own Smart Ads settings, which specifies how I would like to Adsense ads displayed at the top of posts:

1
2
3
4
5
6
7
8
9
10
11
12
13
//This advertisement will appear above your post content. You can surround it by any HTML you like (as shown).
$topad = '
<div style="float:right; padding: 15px 0 0 7px;">
<script type="text/javascript"><!--
google_ad_client = "pub-1419217250584397";
google_ad_slot = "2193850184";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</div>
'; //leave this line

If that looks daunting, it really is nothing more than the code Google gave you when you specified how you wanted your ads to look, with the tiniest bit of HTML (in this case I specified that I wanted the ads on the top right of the post, with the text wordwrapping around the ads, with a little bit of padding in between).

While most people will be looking to use Smart Ads for Google AdSense, it will work just as well with Yahoo YPN, AdBrite or any advertising program. Just add the code you were given. In fact, you could even use them simultaneously.

Once the Smart Ads plugin has been activated and you have properly set up your Google Adsense code, Smart Ads takes care of the ad placement within your posts. You do not have to modify your theme files. If you like, you can specify an additional custom ad which is not governed by the rules you set out. You can add the ad at a specific point in a specific post or merge it into your theme or widgets.

Conclusion

Smart Ads may not have the looks but it certainly got the brains and in this case, that is worth gold - quite literally! Its weak point is really a strong point if you are interested in other advertisement programs and if you don’t, Google’s own Adsense wizards are more than capable of setting up the ads just the way you like.

My rating: 4/5
Versions: WordPress 2.5.1, Smart Ads 1.2

Series of posts, related posts & popular posts

Monday, June 2nd, 2008

One of the disadvantages of a chronologically organised blog is that older posts tend to get burried under newer ones. A shame when your posts do not have a limited shelf life. A similar problem occurs when you write posts that belong to a series of directly related posts, it is difficult for the reader to keep track of all parts and read them in the correct order.

In this article I will look at a few plugins that deal with these issues.

Series of posts

Travis Snoozy wrote the one of a kind plugin In Series, an invaluable tool for stringing together posts in a series. You can easily add, re-order and remove posts from a series from the Write/Edit Post page. It will automatically build a table of contents, previous/next links etc. This does not require you to hack your theme, it works right away. You can of course customize how series information is displayed.

This plugin is a must-have for me. Thanks to this plugin, whenever you add a new post to a series you won’t have to go through the hassle of editing every single previous post in the series and add a link to the latest addition, the plugin takes care of all that in a very elegant way. A huge time saver.

Related posts

There are many plugins available that can generate a list of posts related to the one being viewed, one of those tasks that is hard to keep up-to-date manually. Unfortunately most are out-of-date or have limited functionality, i.e. merely tag matching.

The best plugin I found so far, and one that works with WordPress 2.5.1, is Similar Posts by Rob Marsh. Similarity is judged according to a post’s title, content, and tags and you can adjust the balance of factors to fit your needs. If you like, you can even exclude specific posts or change a pletoria of other options. Pretty need!

This plugin requires an additional library plugin shared by the author’s various plugins and a simple inclusion in your single.php.

Popular posts

Knowing which posts are the most popular of your website can give you great insight in what your visitors are interested in. Perhaps you can write additional posts, provide even more specific information. Or maybe you simply want to publish a list of popular posts. A very popular plugin that can help you with that is Popularity Contest by Alex King. It keeps a count of your post, category and archive views, comments, trackbacks, etc. and uses a balanced formula to calculate a popularity score for each post. Each view has a different weight attached, e.g. clicking a post’s permalink is weighted higher in the overall popularity score than merely viewing the post as shown on the homepage. You can adjust the weights according to your preferences.

The bad news is, the current version of Popularity Contest does not work with WordPress 2.5.1. The good news is, with a few pointers by Ken McGuire, I have patched the plugin to work properly without any further manual action required.

Popularity Contest plugin (patched for WordPress 2.5.1)

Disable RSS feeds for your website

Sunday, June 1st, 2008

WordPress comes out of the box with RSS functionality. Great if you need it, but it lacks an easy off-switch if you don’t. A rare oversight, let’s fix it!

Disable feed creation

WordPress actually supports a number of different feed layouts, controlled by the actions do_feed_rdf, do_feed_rss, do_feed_rss2 & do_feed_atom. These actions can be removed, the easiest way to do this is by adding the following lines to the functions.php file of your theme. It can also be done by writing a new plugin, my preferred method because if I switch to another theme I might forget to move over these changes.

1
2
3
4
remove_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
remove_action('do_feed_rss', 'do_feed_rss', 10, 1);
remove_action('do_feed_rss2', 'do_feed_rss2', 10, 1);
remove_action('do_feed_atom', 'do_feed_atom', 10, 1);

This disables WordPress’s ability to fill the feeds with posts when requested: feeds will be empty and/or return an error.

Disable feed access

We can improve on this slightly by making an optional change in wp-blog-header.php. It is up to you whether you do this or not: it is a core file, so whenever you upgrade WordPress you will have to redo this.

In wp-blog-header.php find:

1
wp();

Add the following lines below:

1
2
3
if (is_feed()) {
   die();
}

Now whenever someone tries to access your feed directly, nothing happens. No error, no nothing. Nothing to see here, move along…

Disable feed notification

Modern browsers like FireFox & Internet Explorer 7 display the feed notification icon in the address bar when a feed was found for the website you are viewing:

It does not make sense to notify visitors of a non-existing feed.

In the header.php file that belongs to your current theme, find and remove the following line:

1
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />

Depending on the theme you are using, there might be additional links to the feed build in. If you use the standard WordPress theme, you should edit the footer.php file.

That’s it, RSS feeds have now been completely disabled on your website. Make sure your clear your browser cache or it might not appear to work.

Downloads

For your convenience, here is a very simply plugin that will disable feed creation. Just put it in your wp-content/plugins directory and active the plugin.

Feed Disabler Plugin

Why I use WordPress

Saturday, May 31st, 2008

never saw the point in using content management systems. Afterall, I could build one from scratch that would do exactly what I wanted. And it would be way cooler! And I did, until I realised that working a full-time programming job and more of the same in all of my spare time is perhaps a bit overkill. Honing my skills is no longer the primary objective: I want to bring the fruits of my creativity out there, not spend all my time building the perfect back office for it.

So I started testing many of the open source content management systems out there. Just to name a few: Drupal, eZ Publish, Joomla, Mambo, PostNuke, TikiWiki, Typo3 and many, many more. Each one has its merits but none left me impressed. Until I tried WordPress.

Pro: installs in under 5 minutes

Once you have that awesome idea for a new website, it is imperative to start writing right away. With WordPress, a fresh new install can be done in less than 5 minutes. Write away and worry about adding features and designing the website afterwards.

Pro: light weight, infinite new features

WordPress features top-notch publishing tools for a blog-like website. Everything you need is right there: posts & pages, categories & tags, RSS, comments, a media library, a well designed content management system and of course a fully customizable website. More importantly, it is relatively light weight: it does not drag around lots of unneeded features ‘conveniently’ build in already and impossible to remove.

Pro: excellent plugin system

Writing plugins for WordPress is easy and fun, at some point you will definitely want to give it a try. With most other systems I usually got impatient doing it ‘the proper way’ or what I wanted simply couldn’t be done. Or I needed to follow some bizarre framework that didn’t make any sense at all. I would start hacking away in the core files, upgrading would from then on become a major hassle. Apart from a rare exception or two, I have never had to resort to hacking WordPress’s core files. Writing a plugin would do the trick instead, without breaking anything.

Because there is such an active community of plugin developers, there is usually a number of plugins to chose from that can do exactly what you had in mind. It is really quite amazing and a major time saver. It usually takes one click to install them. And if the author catches a bug or adds new features you will get a new version notification, again requiring just one click to upgrade. Simply amazing.

Pro: well organised back office, clean & efficient

One of the things that turned me off from using other CMS are poorly designed back offices littered with options, frequently needed ones completely burried under layers of exotic ones. It does not inspire to get the job done. WordPress’s back office works really well. The options you need are there, less frequently used options are hidden by default but still available if you need them. Everything is divided up in a way that makes sense to me. The back office itself is easy on the eyes, particularly well designed.

Cons: your way vs. the WordPress way

There are of course certain advantages to going about it yourself. Every developer has its own personal logic, a certain way of doing things. It might not make any sense to others, but it works for you. Using a system not build by yourself does mean that you will have to get used to another way of getting things done. If WordPress can do 95% of what you had in mind, fixing that final 5% will take you a lot longer than it would have if you had build it from scratch. It is still a lot easier to adopt to though.

Cons: WordPress needs regular upgrades

Using a system that powers many websites attracts attention. Hackers out there are actively searching for bugs to exploit so it is in your best interest to follow upgrade notifications by the WordPress team to the letter. Development is also going at an impressive rate and you will need to keep up: there is pretty good backwards compatibility, but if you delay upgrading long enough you will find that newer plugins no longer work with your version of WordPress. Of course there’s also the issue of older, complicated plugins that are no longer maintained and don’t seem to work with the newest version of WordPress…

Conclusion

WordPress isn’t perfect. While I have seen plugins pulling off major stunts, if your purpose is anything other than a blog/informative website, you really should look for one of the more specific open-source projects out there. Or pick up your programming tool belt afterall. But think twice before you go at it by yourself if WordPress can do the trick for you. Don’t make it about pride: focus on the end result. You’ll thank me later.

Hacking the WP-Syntax plugin

Sunday, May 18th, 2008

Table of contents for Syntax Highlighting

  1. Syntax highlighting plugins
  2. Hacking the WP-Syntax plugin

In my previous article I picked WP-Syntax as my plugin of choice for syntax highlighting. At the time of writing I was using version 0.7. The plugin is not perfect: there are a few issues that need to be addressed. I will do so in this article.

Using the visual editor messes up your code snippets

In order to use WP-Syntax, or any syntax highlighting plugins for that matter, you will have to give up using the visual editor. Which is a shame, because that happens to be one of WordPress’s strong suit. Fortunately, Shantanu Goel devised a quick fix which I have adopted to hack WP-Syntax.

In wp-syntax.php, find:

1
2
3
4
5
6
7
function wp_syntax_code_trim($code)
{
    // special ltrim b/c leading whitespace matters on 1st line of content
    $code = preg_replace("/^\s*\n/siU", "", $code);
    $code = rtrim($code);
    return $code;
}

Replace with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function wp_syntax_code_trim($code)
{
    // special ltrim b/c leading whitespace matters on 1st line of content
    $code = preg_replace("/^\s*\n/siU", "", $code);
    $code = rtrim($code);
 
    $code = strip_tags($code);
    if (PHP_VERSION > 5.0) {
    	$code = html_entity_decode($code, ENT_QUOTES, "UTF-8");
    } else {
      $arrSearch = array("<", ">", " ", "&");
      $arrReplace = array("<", ">", " ", "&");
      $code = str_replace($arrSearch, $arrReplace, $code);
    }
 
    return $code;
}

You can now continue to use the visual editor. Simply add a <pre> block with the lang (and possibly class) attribute and paste the code in the visual editor. WordPress will no longer mess up your code.

The line attribute for line numbering is invalid (X)HTML

This might not seem like a big deal, because it is only used as an instruction for WP-Syntax, it does not end up in the final output. Unfortunately, now that we have hacked WP-Syntax to work in the visual editor, WordPress will add its two cents: it will strip the line attribute automatically to make the code XHTML valid. Fortunately, this is easily fixed.

If you recall, the Highlight Source Pro plugin abuses the class attribute for the same purpose. This is valid XHTML, so WordPress will allow it. We can hack WP-Syntax and swap out line for class in much the same way.

In wp-syntax.php, find:

1
2
3
4
5
6
7
8
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

Change this to:

1
2
3
4
5
6
7
8
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|class=[\"'](\d*)[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

You can now use the class attribute to specify the first line number.

Changing the highlighting colors

WP-Syntax uses the default GeSHi colors for its syntax highlighting. I don’t particularly like them, let’s see if we can do better. Of course, if you don’t like my colors either, feel free to change them!

In wp-syntax.php, find:

1
add_action('wp_head', 'wp_syntax_head');

Add this line:

1
add_action('wp_syntax_init_geshi', 'my_custom_geshi_styles');

Now find:

1
2
3
4
5
6
7
8
9
function wp_syntax_head()
{
  $css_url = get_bloginfo("wpurl") . "/wp-content/plugins/wp-syntax/wp-syntax.css";
  if (file_exists(TEMPLATEPATH . "/wp-syntax.css"))
  {
    $css_url = get_bloginfo("template_url") . "/wp-syntax.css";
  }
  echo "\n".'<link rel="stylesheet" href="' . $css_url . '" type="text/css" media="screen" />'."\n";
}

Add these lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function my_custom_geshi_styles(&$geshi) {
   $geshi->set_keyword_group_style(1, 'color: #0000FF;', true);
   $geshi->set_keyword_group_style(2, 'color: #0000FF;', true);
   $geshi->set_keyword_group_style(3, 'color: #0000FF;', true);
   $geshi->set_keyword_group_style(4, 'color: #0000FF;', true);
 
   $geshi->set_symbols_style('color: #000000;', true);   
 
   $geshi->set_strings_style('color: #808080;', true);
   $geshi->set_numbers_style('color: #808080;', true);
   $geshi->set_regexps_style('color: #808080;', true);
   $geshi->set_escape_characters_style('color: #808080;', true);
 
   $geshi->set_comments_style(1, 'color: #008000;', true);
   $geshi->set_comments_style("MULTI", 'color: #008000;', true);
}

We have now used WP-Syntax’s custom filter to overwrite GeSHi’s default colors. You might want to take a look at the documentation if you are unsure what the various GeSHi’s functions do. You can also take a look at the code comments in the geshi.php file. It also helps to look at the specific language file, for example php.php. You can find it in the geshi subdirectory of the geshi subdirectory (not a typo) in the wp-syntax directory.

Downloads

For your convenience, here is the modified file. Please note that if you are using a different version of the plugin you will need to hack it yourself, do not replace it with this file!

WP-Syntax plugin

Syntax highlighting plugins

Monday, May 12th, 2008

Table of contents for Syntax Highlighting

  1. Syntax highlighting plugins
  2. Hacking the WP-Syntax plugin

One tool us programmers cannot do without is an editor with proper syntax highlighting features. As I will be regularly pasting code snippets on this website, I needed a plugin to do the same thing online to aid in readability. I’d like it to support multiple programming languages, show line numbers and allow me to overrule the default highlighting colors.

The Wordpress Plugin Directory contains a number of code highlighting plugins. I’ve limited myself to the most recent ones, which are certain to work with WordPress 2.5:

I have tried all three and will share my experiences with you.

Highlight Source Pro

This plugin is based on GeSHi (the Generic Syntax Highlighter). Code highlighting is done server-side, so JavaScript is not needed.

I’ve done a test run with this plugin, first with a XML snippet and later with a PHP snippet. I had hoped I could just slap a <pre> block in here and the plugin would pick up which programming language I was using, but unfortunately you will have to specify it yourself by setting the lang attribute. The class attribute can be abused to specify any number as your starting line number. There are no line numbers if you omit the attribute.

The result of my XML test was not satisfactory: it worked, but only the XML tag and the more common attributes like xmlns were highlighted. It should be able to do more with a well-structured XML document. The PHP test fared a lot better: although I didn’t care much for the default colors, my code was highlighted properly. The plugin is intelligent enough to add horizontal scroll bars when needed, which I much prefer over unspecificied line breaks.

WP-Syntax

Like Highlight Source Pro, WP-Syntax is also based on GeSHi. It should therefore work with the languages I use most of the time: CSS, HTML, JavaScript, PHP, SQL & XML.

I again did both tests and it probably comes as no surprise that the results were completely identical, as both plugins use the GeSHi engine. WP-Syntax also supports line numbering, although it uses its custom line attribute instead of HSP’s neater class solution. If you care about XHTML validation, that you should probably skip WP-Syntax or avoid line numbering. I prefer WP-Syntax’s default layout for code snippets over HSP’s.

An issue I ran into that affects both plugins is that WordPress will seriously mess up your code snippets if you dare to use the visual editor. We will have to find a solution for that.

WP Chili

WP Chili is based on Chili - the jQuery code highlighter. Unfortunately I can be very brief about this plugin: it does not work for me at all.

The same two tests once again: XML was not recognised. My PHP example fared slightly better but it did not yield a nice looking code snippet, it looked rather like a <p> tag with color formatting. It did not add horizontal scroll bars when needed. I couldn’t get it to work consistently: a few times it worked, most of the time it did not.

Conclusion

The choice is between Highlight Source Pro and WP-Syntax. There is little difference between the two. Neither does exactly what I want. It should be noted that WordPress plays havoc with code snippets if you use the visual editor. It is dissapointing that neither plugin offers a solution for this problem.

Out of the two I picked WP-Syntax. I will have to make some modifications anyway and WP-Syntax’s default layout is more to my liking. To be continued!