CRON Jobs Give WordPress Users Peace of Mind

If you’ve never had a website go missing, then you’re very lucky. Your host can have a network error or hardware failure that loses your site’s files. A malicious hacker can penetrate your FTP server and replace your site with files of his choice. You might get curious about a new plugin, install it, and break your site to pieces in the process. No words really describe the panic you feel when you realize your website is gone!

If your host allows you to use CRON jobs, you can protect yourself from all those things by running a daily backup of your database and web site files. If you lose your site for any reason, you can put it right back in a few minutes.

A CRON job is script that your web server executes at a specified time. CRON comes from the word chronograph, and it is a time-based job scheduler for LINUX based operating systems. Most WordPress sites are running on such an operating system. If you have access to a CRON scheduler through your hosting control panel or have command-line access to your hosting server, you should be able to write a shell script to back up your database and site files.

The first thing you will need is a shell script. The script will connect to your database, export the whole thing to a file, and zip the file up so you can store it safely somewhere. Then, it will zip up all your website files.

If you have a host panel where you can create and edit a file, you can do this there. Otherwise, open any non WYSIWYG text editor and create a file called The .sh file extension indicates this is a shell script file.

export TERM
NOWDATE=`date +%a` # Sets the date variable format for zipped file: Sun
clear # clears terminal window
echo "Hi, $USER!"
mysqldump --opt -Q -h your-website-host-name --user=your-db-user-name --password=your-db-password your-database-name | gzip -v9 - > /www/public_html/backups/MySQL-$NOWDATE-yourwebsite.sql.gz

The first line is an indicator to the server for which shell processing language to use. Your server might require

#!/bin/bash or something else there. Use echo $SHELL from the command prompt to determine your shell type if you have command line access.

The TERM instruction tells the server what terminal type it is communicating with; in this case, a text terminal.

Next we create a variable named NOWDATE and use a little script magic to set it to be equal to the abbreviation for the current day of the week e.g. Sun, Mon, Tue, etc.

The mysqldump command will “dump” the entire database including create procedures and insert procedures for all the current data.

Replace your-website-host-name with your website host name for your database. You can find this in your wp-config file if you don’t know it.

Replace your-db-user-name, your-db-password, your-db-name with actual values. Again, these values are likely exactly what they are in your wp-config file.

Replace /www/public_html/ with whatever your hosting account’s root path is.

Replace yourwebsite with some meaningful name.

Now, save the file. You need to set the permissions on the file so that it is executable. If your cpanel has a cron scheduler, just add this file to the list of files it runs. If you need to edit your crontab from command line, see this tutorial.

To back up your site files, as well as your database, add a few more lines to your script.

echo "Zipping wordpress directory structure..."
tar -czf $HOME/backups/$NOWDATE.mywebsite.tar.gz $HOME/public_html/*

Hopefully, you do have a directory outside your public web root so that your backup files are stored in a location that is not accessible via the internet.

The tar command will pack the files up into a tarball for you (like a zip archive).

  • Leave a Comment
  • How To: Hack WordPress Theme Template Pages

    The key to being able to display exactly what you want in WordPress is understanding WordPress theme template pages. These are the theme files that display pages, not the ones that perform functions like comments, sidebar, etc. Most of us don’t use the WordPress default theme that comes with installation, and end up downloading a free theme from the Internet. This is a great way to customize your blog, but not all theme authors code their theme the same way. The capabilities of that theme largely depend on how much time the web designer took to code it, in addition to their knowledge of WordPress itself.

    I’m going to explain everything you need to know to be able to customize all your theme pages any way you want, and this will give you enough information to begin coding your own theme as well. Even if you’re an ‘expert’ theme coder, you should learn something new from this article.

    How WordPress Works

    The most important thing you could learn about WordPress is the Template Hierarchy, or – “the order in which WordPress calls pages”. The ONLY file that is required in the PHP files of any WordPress theme is the “index.php”. That’s it! That one file could handle every single function WordPress performs (if you wanted it to). Or, you could have a WordPress theme that had a PHP theme for for every single WP function (or anything in between).

    The Order of Things

    Every time a WordPress page is called the WP ‘engine’, if you will, determines (through process of elimination) what kind of page it is. It’s kind of like a “where am I?” function. WordPress says “what page am I…” and in turn tries to call pages in a specific order. If WP doesn’t find the PHP file it needs it just defaults to the “index.php” file and uses it instead. There are 9 basic kinds of pages WordPress looks for first:

    Am I the Home Page?
    If WP thinks it’s on the home page it will look for “home.php” first, and “index.php” second.

    Am I Post Page?
    (Single) post pages look for “single.php” first, and then default to “index.php”.

    Am I a ‘Paged’ Page?
    (Static) or ‘paged’ pages in WordPress look for a “pagetemplate.php” first (if assigned when published), “page.php” second, and default to “index.php” last.

    Am I a Category Page?
    When WordPress determines it’s on a category page first it looks like a category specific ID page, such as “category-7.php”. If it doesn’t find that it next looks for a “category.php” (which would be used on every category page). If that’s not there is searches for “archive.php”, and last it defaults to “index.php”.

    Am I a Tag Page?
    If WordPress is on a tag page it tries to load “tag-slug.php” first, with ‘slug’ being the name of your tag. If your tag is ‘wordpress hacks’ the tag slug page would be “tag-wordpress-hacks.php”. It that’s not available, WP next looks for “tag.php” which would load for all tag pages, then “archive.php”, and if that’s not there last it defaults to “index.php”.

    Am I an Author Page?
    If your blog has multiple authors, first it looks for “author.php” to display the details. If that’s not there, it tries to load “archive.php”, and last it defaults to “index.php”.

    Am I an Archive Page?
    Archive pages are loaded when WordPress loads a date based page for previous posts. First it tries to load “date.php”, then “archive.php”, and last it defaults to “index.php”.

    Am I a Search or 404 Page?
    If WP determines it’s on a search (results) or 404 (not found) page the it tries to load either search.php or 404.php. If not, the default is once again “index.php”.

    Am I an Attachment?
    Out of all the WordPress theme template pages, the attachment page is probably the one used least, and I have to admit – I’ve not seen a single one of these in any of the hundreds of themes I’ve downloaded. WordPress uses these special pages usually for uploaded content, which would explain why it first looks for “image.php”, “audio.php”, “video.php”, or “application.php”. Then it tries to find “attachment.php” or “single.php”, and if none of those are available it also defaults to “index.php”.

    Inner Workings of WP Theme Templates

    As I said before, you could use a single index.php file to handle the 9 types of pages. You would simply code in some conditional tags, like I showed you in the last tutorial I wrote here on WP Hacks. A single index.php would then just contain code to say if is_home, do this, if is_single do that, etc. That’s a lot of code for just one page, and a bit unorganized – and it doesn’t leave a lot of room for customization.

    Coincidentally, like WordPress searches for 9 basic pages – each theme template page also contains 9 basic WordPress elements:

    1. a header call
    2. opening of ‘the loop’
    3. a call to get the permalink and (some) meta
    4. a call telling WordPress what to get
    5. a call to get either the content or an excerpt
    6. (maybe) more meta
    7. closing of ‘the loop’
    8. a sidebar call
    9. a footer call

    Those are only the WordPress elements, of course the PHP code to make them work is usually scattered throughout the appropriate HTML code make your theme’s layout and graphic design work properly. I’m going to explain these elements a bit more so you can understand how you can customize (or create) nearly any theme template page.

    Header, Sidebar, and Footer calls

    I’m going to handle all 3 of these elements at once, since they are all basically the same. When you see this code in a template:

    <?php get_header(); ?>

    WordPress is simply opening the “header.php” file. The same is true for get_sidebar (sidebar.php) and get_footer (footer.php). You could have multiple headers, footers, or sidebars, see the earlier link above for conditional tags.

    Opening of “the loop”

    The infamous “WordPress Loop” is when a call goes out to the database to do something until WordPress says “stop”, i.e. ‘get me the most recent full text posts in their entirety’. The structure of ‘the loop’ changes depending on what kind of page your displaying, and each of the 9 basic types of pages WordPress tries to load has a ‘loop’.

    The opening of the loop generally looks like this:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    You may see it broken down with have_posts on one line to define conditional tags with the while and the_post on another, but it’s still the opening of the loop, and it’s pretty much the same in all pages. One way to use the multi-line loop opending is to place a parameter between “if have_posts” and the rest by using query_posts in between to show only a single post, posts from a time period, the last post only, posts from certain categories, or even change the ordering of posts being iterated in the loop.

    A Call to Get the Permalink and (some) meta
    The very last section of the loop opening (the_post) actually makes individual data available through each iteration of the loop. This data referred to usually as “post meta” because it’s descriptors and identifiers for the individual content being looped through. Typically things like the permalink (URL), title, date, etc. I say ‘some’ meta, because most themes show some things before the individual post content, and then some after – such as categories and tags.

    Here’s a short list of things you can call in post meta: the_permalink, the_ID, the_title, the_time, the_author, the_author_email, the_author_posts_link, the_category, single_cat_title, the_tags, single_tag_titls, edit_post_link, comments_popup_link, comments_rss_link

    Example code you might see for post meta would be something like this:

    <div class="post" id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

    A Call Telling WP What to Get
    Next WordPress will decide how much of the actual individual post content to get for you. How much is gathered from the database depends on whether your look uses “the_content” (to get it all) or “the_excerpt” (to get part of it).

    (Maybe) more meta
    As I previously mentioned, the common things to see after a post are assigned categories or tags, and sometimes you see an “edit” link here as well. Some themes even put date published meta after the post content.

    Closing of ‘the loop’

    The code looks like this:

    <?php else : ?>
    <?php endif; ?>

    Typically it’s on more than one line in case you want to build an option in, such as a message “Sorry, we didn’t find anything”. After the sidebar, before the sidebar and footer calls, is where you typically find the “next” and “previous” navigation links.

    Bastardized Loops?

    Well, just because most loops look like the examples I just gave you, doesn’t mean you can’t bastardize them in just about any way you can imagine. I recommend you read the WP Codex page The Loop in Action for examples of archive, category, and single post formats – as well as static home page.

    The Codex official page for the loop has several examples of how to place multiple loops in one page.


    Armed with just a tiny bit of knowledge, you can hack just about any WordPress theme template page to do just about whatever you want! Now that you understand (in great detail) how WordPress calls it’s pages and how the loop works, you can conquer any task! Have fun customizing your blog’s theme!

  • Leave a Comment
  • How To: Use WordPress Conditional Tags to Hack Your Theme

    By using simple conditional tags – it’s pretty easy to add some very basic hacks to your WordPress theme to have more control over what’s displayed when.

    Here are some things you could do with a conditional tags:

    • Display something only on certain pages
    • Display something only on certain categories
    • Display something in header and footer only at certain times
    • Display something only on sub-children of particular pages
    • Display something only in the WP dashboard
    • Display something in the sidebar only when certain conditions are met
    • Do something only when there’s a “sticky” post
    • Do something only when a “page template” is used
    • Do something only for “author pages”
    • Display something only on search or 404 pages

    Let’s say you want something to display only on the homepage, or just category pages, or maybe just your 404 (not found) page – it’s quite easy to do. You don’t have to be a hardcore programmer (I’m not for sure) to implement these very simple theme hacks.

    Where to Use Conditional Tags

    It all depends on your your theme is structured. I’ve seen WordPress themes that a single “index.php” file handle just about everything, and other ones that use only the home.php, index.php, single.php, archive.php, and category.php files. You could handle everything with a bunch of code in one file if you want using conditional tags, have individual files for each thing, or any combination in between.

    Most themes I’ve encountered usually have an index.php and a single.php only. If you want to know what pages WordPress looks for first in a theme before defaulting to the “index.php”, read the official WordPress Template Hierarchy page.

    Conditional tags are great because you can use them both in and out of the loop. You can use them directly in theme pages, but you could also use them in your header, footer, comments, and sidebar files. Actually the sky is the limit, and you have only your own imagination to limit you!

    Conditional Tag Examples

    The worst thing I see on most posts about conditional tags is that they don’t have a lot of examples for you to draw from – so I’ll try to give you a few ideas to get you started…

    If This is XYZ page

    One of the most common ways to use a conditional tag is to add a filter of sorts to tell WordPress “if I’m on ‘XYZ’ WP page – then do this. The most common reason would be maybe to show certain things (ads, text, messages, graphics, scripts, flash) in certain places.

    For example, let’s say you want to display a message to visitors on your home page only…

    <?php if (is_home()) { ?>

    Welcome, you will only see this message on my homepage!

    <?php } ?>

    replace “is_home” with “is_front_page”, “is_single”, “is_sticky”, “is_page”, “is_page_template”, “is_category”, “is_tag”, “is_author”, “is_date”, “is_archive”, or “is_attachment” to make your message show up on nearly any WordPress page. Remember, once the condition is met, you can “do” anything – from including a file to showing special graphics, running a script, anything! You could use this condition for example to show an ad on just your homepage, or just single pages.

    If this is XYZ page show this, else show that

    The nice thing about conditional tags is the fact that you can have as many conditions as you want…like this:

    <?php if (is_home()) { ?>

    <p>Show this!</p>

    <?php } elseif (is_single()) { ?>

    <p>Show this instead!</p>

    <?php } elseif (is_category()) { ?>

    <p>Show something different!</p>

    <?php else { ?>

    <p>Show this if no conditions are met</p>

    <?php } ?>

    OR – you could even structure it in such a way that you lump some conditions together like this:

    <?php if (is home() || is_single() || is_category() || is_page()
    || is_archive() { ?>

    <p>Show this on all those pages!</p>

    <?php } ?>

    The double-pipe or || in the code signifies “OR”, so WordPress knows, if this is home, or a single page, or a category page, or a “page” page, or an archive page – then so something.

    Show Everywhere, Except…

    Sometimes you want to show something everywhere possible except just one or two places…

    <?php if (is_home()) {
    else { ?>

    <p>Show this everywhere!</p>

    <?php } ?>

    With this code we just say if it’s “home” do nothing, else show do this. You could add multiple conditions (exclusions) to this using the || OR operator as in previous examples.

    Getting even more specific

    One thing I hadn’t mentioned was that you could pass additional parameters to the conditional tags for even finer grained control. For example, instead of targeting are single post pages with “is_single”, you could actually target just one using any of these formats:

    is_single(’25’) // uses posts ID
    is_single(‘Title of my post’) //uses the exact title of the post
    is_single(‘title-of-my-post’) //uses the permalink of the post
    is_single(array(25,’this title’,’this permalink’)) //uses when any of the 3 are true

    You can use similar parameters for paged pages, template pages, categories, tags, etc. The official WordPress Conditional Tag page in the Codex lists them all.

    Force WordPress Functions for Certain Conditions

    Have you ever not wanted to add people to your blogroll because their link will display on EVERY SINGLE page of your WP powered site? That’s an easy hack with conditional tags, because you could hack your sidebar to display your blogroll ONLY on your homepage like this:

    <? php if (is_home()) {

    You could modify this to display just about anything in the sidebar for whatever conditions you want. Let’s take this a bit further though – let’s say that maybe I want a special header or footer when certain conditions are met? You can do that too…

    <? php if (is_home() || is_single() || is_page()) {
    elseif (is_category() || is_tag()) {
    include (TEMPLATEPATH . '/header2.php');
    elseif (is_404() || is_search())
    include (TEMPLATEPATH . '/header3.php');
    } ?>

    Using that example code you could have as many different headers, footers, or sidebars as you wanted and you could include them for whatever conditions you specified. Just replace get_header with get_footer or get_sidebar, and edit the rest include the files you want.

    Using Conditional Tags to Change Styles

    So far my conditional tag examples have been to show you how code conditions to do this or include that. Another very simple (and powerful) was to use conditional tags is to just use them for coding style. You could have any element (paragraph, div, heading) change colors or font size or anything related to style when certain conditions are met.

    Let’s take a very simple example, maybe your pages have content contained within one div like this:

    <div id="content" class="main">
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>

    In most themes, that main “div” is styled the same way on EVERY single page of the theme. Maybe you want it styled one way for your homepage, but another for paged pages, single posts, archives, etc. What you do in this case is to write different classes in your stylesheet for each and call them something like “single”, “archive”, and “paged” – and then you code it like this:

    <div id="content" <?php if (is_home()) { ?> class="home"
    <?php } elseif (is_page()) { ?> class="paged"
    <?php } elseif (is_single()) ?> class="single"
    <?php } elsif (is_archive()) { ?> class="archive" <?php } ?> >
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php endwhile; ?>

    You use any variation of this to control any element of your theme at will under nearly any condition. This would also be a great way to control your Post title and meta and have it display different things on different parts of your WordPress powered site.


    I think mostly that the power of conditional tags is widely underutilized. Many of us just take for granted the way a theme works and looks, and forget that with just a few strokes of code and some imagination – we can change just about anything under the sun in our WordPress theme! I’d like to see some comments on what kinds of things you’ve used conditional tags for in your WordPress site (code samples welcomed!).

  • Leave a Comment
  • How to: Using Thumbnails in your Sidebar “Recent Post” Listing

    On the WP Recipes forum, a guy just asked me how I managed to insert thumbnails on the recent post widget located on the sidebar of my blog Cats Who Code. That’s a lot simpler than it seems.

    If you’d like to see a live demo of this tutorial, just click here.

    1) – Make sure you’re using a custom field in your posts, with the thumbnail url.

    There’s a lot of tutorials available online to add a custom field to your pots. This one is good in my opinion, so you should read it if you don’t know how to define a custom field.

    2) – Download the required plugins

    In order to customize your recent posts widget, you have to download the Recent Posts plugin by Sebastian Schmieg.

    You’ll also need to download and install the Post-Plugin Library, which contains all the php functions needed by the Recent Posts plugin.

    3) – Upload and activate the plugins

    Once you downloaded the plugin and the library, unzip the files and upload it to your wp-content/plugins directory. Then, login to your WordPress dashboard, Go to “Plugins” and activate both the library and the Recent Posts plugin.

    4) – Configure the plugin

    After having the plugin and the library uploaded and activated, you have to configure the options so you’ll be able to display the post thumbnail on your sidebar.

    To do so, login to your WordPress dashboard and navigate to “Settings”, “Recent Posts”.
    On “General”, you can tell the plugin how many posts to be displayed, if it should display posts and pages or only posts, and a lot more.

    Once you configured the “General” options to fits your needs, go to the “Output” options. In order to display your custom field thumbnail on your sidebar, you have to enter a bit of html as well as the special parameter to embed the custom field.

    Here’s the code that I entered:

    <li><img src="{custom:Image}" width="48" height="48" alt="" />{link}
    <div class="clear"></div>

    The special parameter {custom:Image} tells the plugin to embed the Image custom field. Don’t forget to replace Image with the name of your custom field!

    5) – Add the widget to your sidebar

    Simply go to “Design”, “Widgets” and add the Recent Posts + widget to your sidebar. Visit your blog, your thumbs are showing in the sidebar along with your recent posts!

  • Leave a Comment
  • How to: Upgrade your WordPress Blog in a Minute via SSH

    Having to upgrade your WordPress install every time a new version is released is necessary, but also very boring. If you have a SSH access to your server, here’s a very quick way to upgrade your WordPress install.

    This is probably the most important step of this whole tutorial: always backup your WP Database and files. This tutorial have been tested, however I won’t be held responsible for any kind of data loss.

    Once done, you can connect to your server by using SSH. On Mac and Linux platform you can use the Terminal, on Windows I recommend using Putty.

    Get the latest WordPress version available:


    Uncompress it:

    tar xfz latest.tar.gz

    Delete the wp-admin and wp-includes directories:

    rm -rf ./wp-includes/
    rm -rf ./wp-admin/

    Go to the wordpress directory:

    cd wordpress/

    Now, it’s time to copy the downloaded files to your existing WP install, by overwriting old files:

    cp -rpf -f * ../

    Once done, come back to the WordPress directory and remove the downloaded files:

    cd ..
    rm -rf ./wordpress/
    rm -f latest.tar.gz

    Visit your blog and upgrade the database (if needed). That’s all, your blog is now up to date and the whole process didn’t took more than a minute or two!

  • Leave a Comment
  • How To: Building a Google Custom Search Engine (GCSE) into Your WordPress Blog

    Over the holiday weekend I decided to spend some time cleaning up a few of my websites and while doing this, I ended up installing Google’s Custom Search Engine on a couple of my WordPress blogs (as of this post I haven’t done this on WP Hacks yet, but will probably end up doing so here as well).

    For people wondering how to easily install Google’s Custom Search Engine (GCSE) on their WordPress blog, here are two resources I recommend:

    • Build Your Google Custom Search Engine – The first step is to actually create your Google Custom Search Engine.   The post by Maki over at Dosh Dosh does a GREAT job of explaining how to do this.   Towards the end Maki explains how to actually integrate it into your WordPress blog, but rather than go through all those steps, you’ll want to instead check out the second step.
    • Integrate your Google Custom Search Engine into WordPress – Once you’ve built your custom search engine, head over to this post and figure out a quick and easy way to integrate it into your WordPress blog.   This method ONLY requires you to alter the searchform.php and search.php files (which most themes come with), so it is really easy.

    Why Use Google’s Custom Search Engine?

    With the current setup of the WordPress search engine, posts are displayed with the most recent post first based upon the search term.   Using Google’s Custom Search Engine, it will use Google’s search algorithm to display your posts with the most relevant post on top and you can actually integrate it into your WordPress blog, so it looks natural.   Using this also allows you to display Google AdSense ads, which can earn you money on some blogs.

    The main downside with this method is that Google has to actually index the post before it will appear in your blog’s search results, but that won’t be a problem for most blogs.  It will just add a small delay before posts appear in your search results.

  • Leave a Comment
  • Fixing the Unresponsive Script Problem in WordPress 2.5+

    Yesterday in my post about moving your blog to a new domain, I mentioned that there is a problem that presents itself when posts are imported into WordPress 2.5 or later.   A quick search determined that I wasn’t the only one having this problem, so I decided to document how to fix this problem here (or at least how I was able to fix it).

    Here are the steps I took to fix the problem with the unresponsive script in the Write panel (requires CPanel/PHPMyAdmin access):

    1. Please make a backup of your database in case you run into any problems.  These are the steps that worked for me to fix the problem, but I can’t guarantee they will work for everyone. 
    2. Login in to your CPanel.   This is usually found at and usually uses your FTP password.
    3. Access your PHPMyAdmin (in CPanel this is usually found at the bottom of the MySQL Databases page).
    4. Click on the MySQL database you created for your new WordPress installation in the menu on the left.
    5. Click on wp_postmeta in the list of tables on the left.
    6. Click the Browse icon.
    7. Click on the post_id tab once to sort the list from 0 to higher.
    8. Now you’ll need to delete anything with a post_id of 0.  Depending on how many posts were imported, there may be a lot of zeroes.  I had about 600 of them to delete.

    The best way to do this is to locate the boxes with “row(s) starting from record #” between them.  In the front box put 100 (or whatever number of your choice) and always put 1 in the right box.   Click Show.  You can use the select all button and verify all of them are zero.   Uncheck any that aren’t zero and click Delete.  Repeat until all zeroes are gone.

    Now open your Write panel in your new blog’s dashboard and everything should be good!

  • Leave a Comment
  • How to: Display your WordPress Tags in a Drop-Down Menu

    Tags are useful to any blog: As you know it, they allow the user to display a list of posts related to a subject.
    Most of the time, tags are displayed with in a tag cloud. If you have 20 different tags, that’s ok, but if you have 100 or more tags your tag cloud will be very hard to read, and no-one will click on it.

    This is probably why many blogs recently stopped displaying their tag cloud, or put it on a separate page. But I got another solution.

    My wife owns a blog where she reviews books. She use tags to display author names, which is a great use of tags, in my opinion. The tags were first displayed as a list, and it was perfect. But after she reviewed 60+ books, the list started to be too long.

    She asked me about a solution, and after some reflection, I came up with the idea of using a drop-down menu (select html element + a bit of javascript). Happily, I was able to found this code on WordPress forums.

    First, we have to create a php function. Copy and paste the following code in the functions.php file of your theme (Be careful with the php opening/closing tags!)

    function dropdown_tag_cloud( $args = '' ) {
    	$defaults = array(
    		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
    		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
    		'exclude' => '', 'include' => ''
    	$args = wp_parse_args( $args, $defaults );
    	$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
    	if ( empty($tags) )
    	$return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
    	if ( is_wp_error( $return ) )
    		return false;
    		echo apply_filters( 'dropdown_tag_cloud', $return, $args );
    function dropdown_generate_tag_cloud( $tags, $args = '' ) {
    	global $wp_rewrite;
    	$defaults = array(
    		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
    		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
    	$args = wp_parse_args( $args, $defaults );
    	if ( !$tags )
    	$counts = $tag_links = array();
    	foreach ( (array) $tags as $tag ) {
    		$counts[$tag->name] = $tag->count;
    		$tag_links[$tag->name] = get_tag_link( $tag->term_id );
    		if ( is_wp_error( $tag_links[$tag->name] ) )
    			return $tag_links[$tag->name];
    		$tag_ids[$tag->name] = $tag->term_id;
    	$min_count = min($counts);
    	$spread = max($counts) - $min_count;
    	if ( $spread <= 0 )
    		$spread = 1;
    	$font_spread = $largest - $smallest;
    	if ( $font_spread <= 0 )
    		$font_spread = 1;
    	$font_step = $font_spread / $spread;
    	// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
    	if ( 'name' == $orderby )
    		uksort($counts, 'strnatcasecmp');
    	if ( 'DESC' == $order )
    		$counts = array_reverse( $counts, true );
    	$a = array();
    	$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
    	foreach ( $counts as $tag => $count ) {
    		$tag_id = $tag_ids[$tag];
    		$tag_link = clean_url($tag_links[$tag]);
    		$tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
    		$a[] = "t<option value='$tag_link'>$tag ($count)</option>";
    	switch ( $format ) :
    	case 'array' :
    		$return =& $a;
    	case 'list' :
    		$return = "<ul class='wp-tag-cloud'>nt<li>";
    		$return .= join("</li>nt<li>", $a);
    		$return .= "</li>n</ul>n";
    	default :
    		$return = join("n", $a);
    	return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );

    Once you have your function, you must call it somewhere on your theme. Just open the file where you want the list to be displayed (Most of the time it is sidebar.php) and paste the following code:

    <select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
    	<option value="#">Liste d'auteurs</option>
    	<?php dropdown_tag_cloud('number=0&order=asc'); ?>

    Now, you have a very cool drop-down list to display you tags. No more unreadables tag clouds!

  • Leave a Comment
  • Parsing and Hacking External RSS Feeds in WordPress

    It’s a little known fact that WordPress contains it’s own RSS parser, and you can use it to your advantage to make custom dynamic pages!

    This is a tutorial on how to take an ordinary RSS feed, parse it using WordPress’ internal RSS parser, and then create dynamic pages with it. I’m not talking about being a scraper, creating a splog, or doing anything spammy at all. I’m talking about creating added value for visitors and your blog while making money. We all know what a “splog” is. It’s a “spam blog”…or someone who takes RSS feeds and generates pages and posts from them and tries to profit using say Adsense or affiliate ads. The pages are just excerpts and titles from RSS feeds (sometimes full posts), and while they do link back to your site (usually) – they’re not the type of links you want to get. These spam blogs don’t have any redeeming value at all, no contact information, and certainly no original content at all.

    Now – let’s take your blog, let’s say you have a birdwatching club in Buffalo, New York. You’ve developed a great site with loads of original content, bunches of articles, and your members visit regularly. The two things that you and other members talk about regularly are binoculars and digital cameras. This is what you could do…

    Craigslist is America’s garage sale. If you don’t live in America, I’m sure something like this is in your region. Craigslist offers RSS feeds on each and every page. Here’s how the birdwatching club could benefit from that.

    First – create a page template. From your WordPress theme directory, download “single.php” (or the theme template that creates post pages) to your desktop. Open it in Notepad or any text editor, and add the following lines of code to the top:

    /* Template Name: Craigslist Template */ ?>

    This is the tricky part, because each and every theme is different. You need to look for the following block of code (which is what prints the page content out):

    <div class="entry">
    <?php the_content(__('Read more &raquo;')); ?>

    Right after those lines (or “the loop” in your template, you want to copy and paste this code:

    <?php include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('', 5); ?>

    That code comes from this page in the WordPress Codex. WordPress has a built in “RSS Parser” that will read any RSS feed and spit out a list of links is list ordered fashion to a page. So, if you visit the Buffalo Craigslist Photo page, you’ll find at the bottom of the page a little orange RSS icon. Click on that link, and then copy the URL, and paste it over the example URL in your template. The number (20) that comes after the URL is the number of items from the RSS feed to parse and display. Save the template, and upload it back to your theme directory of your WordPress blog.

    In your WordPress Dashboard, go to “Write -> Page”. Type some original content in the box, and scroll down to the bottom (in WordPress 2.5, on the sidebar in <2.5) and select “Craigslist Template”. Title your page “Craigslist”, and then save and publish the page. Now if you go to you should see a listing of things from craiglist with your original content on top.

    Look what you did, now the Buffalo Birdwatchers Club won’t can look at photo equipment right on the member web site without having to go to Craigslist unless they see an item they like. You can add multiple feeds to the same page, just copy and paste that code block in multiple times with different RSS feed URL’s. Just remember, the more your add, the longer they take to parse, and the longer the page will take to load. I loaded 6 feeds @ 20 items per feed, and the page took about 20 seconds or so to load.

    This Craigslist example is just that – an example. If you want to see how this looks in realtime, look at this Craiglist RSS example I just built. You could use RSS feeds from just about anything, digg, google news – any site that has a feed. As long as you’re only using headline feeds and not stealing content – nobody should ever think you’re plagiarising them in any way, you’re sending them traffic. In your template file, before you parse the feeds add a few paragraphs of original content about what they user might find (on Craigslist – or whatever) and now you have a page with ever changing (daily) content, AND orignal content that google can index!

    Now, how do you monetize this? Well, use your noggin and add some affiliate banners, adsense, or other items above, below, or in your sidebar. There is a better way to monetize without using these though…

    eBay has RSS feeds – don’t they?  All you have to do is add your affiliate code to an eBay RSS feed, and you could use it instead of the Craigslist feed for a list of eBay items, and every time someone clicks, bids, and buys an auction you profit! First you have to be a member of the eBay Partner Network (EPN). Once logged in go to “Tools->Widgets->RSS Feed Generator”. Then just enter in what you’re looking for. Like Craigslist, you can “geo-target” what you want by scrolling down and checking “Items within 50 miles of xxxxx zip code”. Then click “search” and the page you get should be auctions for only that area. Again, scroll to the bottom of the page and see the orange RSS icon. Click on it and copy the URL and use that as the URL in the code block in your template, save the page now your blog page will list eBay auctions! The difference between these listings and Craigslist is that you profit from each won auction click on these.

    I’ve just given you a few ideas, the sky’s the limit with these. You could probably do the exact same thing with Amazon. Now, there’s one thing I didn’t tell you, often people want to know how to take an RSS feed an automatically create blog posts from the feed and publish them, kind of like what’s called an “auto-blog”. Let me state first that there are very few legit reasons to do this, because of duplicate content issues in google, etc. Most people want to use this to try and create quick spammy blogs with no original content, but there are (just a very few) legit reasons to want to create posts from an RSS feed (like doing a blogroll type page for members of a larger community). You can do that easily (and freely) with RadGeek’s Feed WordPress Plugin.

    Now, get hacking, come back to comment and tell me what you’ve come up with!

  • Leave a Comment
  • Top 5 WordPress Navigation Menu Tutorials

    Navigation menu is a really important part of a website, both visually and functionally. Though, I noticed that most blogs use a really simple navigation menu. A simple menu can be good sometimes, but if you’re looking for something more sophisticated, I’m pretty sure you’re going to enjoy this list.

    Here’s 5 really cool tutorials to help you create a stunning navigation for your WordPress blog.

    Creating Two-Tiered Conditional Navigation in WordPress

    Which WordPress user doesn’t know Darren Hoyt, the creator the free magazine theme Mimbo Magazine ? In this great tutorial, Darren explains how to create a professional two-tiered navigation menu. Source

    How To: Making a Categories Drop-Down Menu

    This was my first post on Hack WordPress, and seems like you guys enjoyed it. In this tutorial, I explain how to create a navigation bar with a category drop-drown menu. Source

    WordPress: “Magazine style” Horizontal dropdown menu

    One of my last tutorials, published on my blog CatsWhoCode. This how-to explain how you can create an horizontal dropdown menu. Source

    Using Sliding Doors with WordPress Navigation

    This sliding doors CSS hack allows you to create sophisticated tabs for your navigation bar. Sadly, WordPress core functions wp_list_pages() and wp_list_categories() don’t allow you to add the required span tag to use this technique.
    This tutorial will show you how to bypass this limitation by using a hack, and then you’ll be ready to enjoy sliding-doors for your WordPress navigation! Source

    Any other navigation tutorial that should be in the list? Leave us a comment below!

  • Leave a Comment