CRON Jobs Give WordPress Users Peace of Mind

This guest post was written by Jennifer Nodwell, a coder and website developer since 1997 who works almost exclusively with open source CMS applications like WordPress. If you have webmaster or WordPress knowledge and are interested in writing a post for WordPress Hacks, please contact us.

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 daily_backups.sh. The .sh file extension indicates this is a shell script file.

#!/bin/sh
TERM=linux
export TERM
NOWDATE=`date +%a` # Sets the date variable format for zipped file: Sun
clear # clears terminal window
echo
echo "Hi, $USER!"
echo
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
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).

How To: Add Google Rich Snippets to WordPress (Without Editing Your Theme)

This guest post was written by John of WordPress Expert, where he provides WordPress tips, tutorials, news, plugins, and more. If you have WordPress knowledge and are interested in writing a post for WordPress Hacks, please contact us.

When searching the web with Google, have you ever noticed that certain webpages with product reviews have a little star-rating and additional info that appears underneath the title?

For example…

Rich snippet example

Notice the additions under the hyperlinked title. These eye-catching additions are called “rich snippets.” Rich snippets give additional prominence to your review pages when they appear in search results and could help garner additional search engine traffic for your site.

You can ask Google to show this sort of data for your review posts by adding hReview code to your WordPress blog. This process has been covered in other tutorials before, but previous methods required you to edit your theme’s code and fiddle with custom fields to get it to work. Not anymore — here’s the easier, plugin-only method:

  1. Install the SEO Ultimate plugin. (You can download the zip file here or you can go to the SEO Ultimate homepage and enter your blog’s URL in the Auto Installer field.) Activate the plugin once it’s installed. SEO Ultimate has many other SEO features besides rich snippets, but if you just want to use the rich snippet functionality, you can disable everything else under the “Modules” section of the plugin’s “SEO” menu.
  2. In the WordPress administration interface, find a post that you’d like to mark as a review and open it in the WordPress editor.
  3. In the “SEO Settings” box under the content editor, select “Review” from the “Rich Snippet Type” drop-down. (If your post has a category or tag called “Review” or “Reviews,” SEO Ultimate will pre-select the “Review” option automatically.)
  4. If you gave a rating to the product you reviewed in your post, select the most-applicable star rating from the drop-down.
  5. Click “Save Changes” to save your post. All done! If you want, you can put your post URL through Google’s testing tool to see a preview of your new rich snippets.

SEO Settings box

Following these steps will tell SEO Ultimate to add the hReview code to your reviews. (Obviously, only add the code to posts in which you actually review something.)

Note that according to Google’s FAQ, adding the code by itself won’t guarantee that Google will show rich snippets for your site. However, you can request that Google display rich snippets for your site using this form. Even if Google doesn’t show your rich snippets right away, having the code on your site ahead of time will help ensure you’re ahead of the game if/when Google rolls out rich snippets on a wider scale.

Enjoy your rich snippets!

How To: Add a Twitter Link to Your WordPress Blog

Twitter is all the rage these days and it doesn’t seem like it will be going anywhere any time soon.  With that said, it often surprises me that many WordPress blog owners  don’t offer a convenient way for their readers to retweet their content.  Anyone can grab a Twitter WordPress plugin to tweet their new content as it is published, but what about your older content?

Rather than passing up all that potential traffic, I’ve found that offering a link somewhere within your post (optimally at the bottom of each post) is a great way to help your readers and incoming search engine traffic to promote your content for you.  When people find great content they like to share it with others, so why not make it easy for them?

Not only is adding a “Tweet This!” link a great choice, but it is really easy to do.  Chances are if you do a search on Google for code to use you’ll find something like the following:

<a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Click to send this page to Twitter!" target="_blank">Tweet This!</a>

This code works just fine, but is not the most optimal solution in my opinion.  Depending on the permalink structure your WordPress blog uses, combined with the length of your domain name, it may be difficult to fit the link into a 140 character tweet.  It also doesn’t leave room for the person to add their own comments to the tweet.

As a proposed solution, I recommend using some WordPress code like the following:

<a href="http://twitter.com/home?status=RT @HackWordPress <?php the_title ();?> <?php echo get_settings('home'); ?>/?p=<?php the_ID(); ?>">Tweet This</a>

This code will automatically insert the “RT” and your Twitter account name (the above example uses our Twitter account, @HackWordPress) then use the ID form of your post with the tweet.  When people click the link in the tweet, they will then be redirected to the actual post using your blog’s selected permalink structure, making a convenient and typically short URL.

Have you integrated Twitter into your WordPress blog? Share your strategies in the comments!

Page Sensitive Multi-Level Navigation

While most sites don’t need incredibly deep page navigation there are situations that justify a hierarchy beyond the typical 2 – 3 levels.  Unfortunately that can be cumbersome for top navigation drop-downs (more than 1 level of drop down is too much IMHO) so another solution needs to be found.  I ran into just such a situation for a client and while I”m also not a fan of left hand navigation it was the decision of the client to utilize it in conjunction with their top navigation, and in retrospect it made sense for them. To keep things easily navigable we also implemented breadcrumbs (which is a good practice anyway).

The mission was to display sub-pages of the current page you are on in the left nav and once you hit the bottom of the hierarchy to show pages which are parallel to that page within the same branch of the hierarchy.

After some digging and experimentation I came up with the following which executes perfectly in only a few lines of code.

<?php
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
if ($children == "")
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1");
?>
<ul>
<?php echo $children; ?>
</ul>
<?php endif; ?>

Of course you style to taste…

That’s it! Used in conjunction with a standard WordPress top-navigation and breadcrumbs you can easily display page sensitive multi-level navigation for your super-complex multi-level site!

Security Reminder: Upgrading Your WordPress Blogs

While I was away over the weekend, it appears that a large number of bloggers who use WordPress have been hacked and a lot of damage has been done.  It seems this problem has shown up for a large number of people, including some very high profile bloggers.  Among them was Robert Scoble, whose blog was among those websites which were hacked.   Damages on Scoble’s site included porn information being placed in old posts, 2 entire months of content being deleted, and more.  Of course the porn then led to his blog being completely banned from Google!   Scoble is not the only one having these problems, however, and even lesser known bloggers have been attacked.  You can read more in this WordPress support forum thread.

If you are wondering what the one thing all of these WordPress sites have in common, the problem is they were all using old versions of WordPress.   As someone that owns and operates well over 100 WordPress installations, I certainly understand the pain it can be to upgrade to the latest version of WordPress every time a new release happens, but I hope this goes to show why it is so important to take the time to upgrade all of your WordPress installations be using the most recent version of WordPress.