Category Archives: Tips

Using cron to Trigger wp-cron.php

WordPress was designed to run in various hosting environments without additional configuration through targeting the lowest common denominator — shared Linux hosting running php4. Since most hosting companies do not provide support for cron and configuring cron can be imposing to the average user, WordPress includes its own version of “cron” for scheduling tasks, which relies on a visitor request to trigger the execution of wp-cron.php.  This approach in essence occasionally hijacks a request for content to execute scheduled tasks, which is less than ideal and does not offer much in the way of control to the sysadmin.

For the more experienced sysadmin/developer that uses more sophisticated hosting (VPS, dedicated server, or even shared hosting with cron support) there is another option: using cron to execute wp-cron.php. First, wp-cron needs to be disabled in the configuration section of wp-config.php.

define('DISABLE_WP_CRON', true);

Then, simply request wp-cron.php directly using cron. For more information on cron, the Wikipedia article is a good starting point.

To edit a user’s crontab, log into the server via ssh and issue the following:

$> crontab -e

If you don’t use ssh often, you may also need to set the $EDITOR environment variable to an editor that you are comfortable using (e.g. vim, emacs, nano, pico, etc…).

There are two different approaches that can be used to execute wp-cron.php.

1. Use wget or curl to issue a HTTP request for wp-cron.php

*/30 * * * * wget > /dev/null 2>&1

2. Write a small wrapper script to setup the environment and execute wp-cron.php directly.

*/30 * * * * /usr/bin/php /var/cron-scripts/run-wp-cron.php /dev/null 2>&1
chdir('/var/www'); // WordPress install DocumentRoot

In both examples, ‘/dev/null 2>&1’ is used to send both stdout and stderr to /dev/null which discards any output and errors. By default, cron will send an email to the email address defined for the user.

Using Page Templates to be More Expressive

Everyone has heard the term “web page.” To even a novice WordPress user, creating and editing web pages just makes sense. For developers of custom themes, pages have expressive capabilities that exceed their cousin the standard WordPress post.   Pages are organized in a hierarchy, and users control which template is used to render the page.

Page templates are powerful.  They give developers control over how the content is rendered, what content appears on the page, and when applied to multiple pages function as a basic label.

Creating an Author Profile Template

Let’s say that we are running a blog with multiple authors contributing content.   Within the “About” section, we want to create profile pages for each author that include a dynamic listing of the author’s most recent posts.  To start, duplicate the normal page.php template and rename the file author-profile.php. For the template to show in the WordPress list of page templates, we need to name the template.

Template Name: Author Profile

The next step is to create a secondary query that lists the five most recent posts by the author. For more information on the parameters that control how the query is initialized see the WordPress Codex.

<?php $author = get_post_meta($post->ID, 'author_profile_name', true); ?>

<?php if (!empty($author)) : ?>
		$args = array(
			'post_type' => 'post',
			'post_status' => 'publish',
			'posts_per_page' => 5,
			'author_name' => $author,
			'paged' => false

		$author_query = new WP_Query($args);

	<?php if( $author_query->have_posts() ) : ?>
		<?php while( $author_query->have_posts() ) : $author_query->the_post(); ?>
		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
		<?php endwhile; ?>
	<?php endif; ?>
<?php endif; ?>

For this to work properly, a custom key named “author_profile_name” must be created and the author name supplied as the value, which is not exactly user-friendly. Since the page template functions also as a label, we can use this information to create a meta box that is only loaded when the “Author Profile” page template is applied. The meta box includes a simple drop-down that includes all blog authors.

Note: This is just a barebones example that should be secured using a nonce and permissions check.

function gc_author_meta_box_profile($post) {
	$current_author = get_post_meta($post->ID, '_author_profile_id', true);
	echo '<label for="author_profile_id">Profiled Author</label> ';
	$args = array('id' => 'author_profile_id', 'name' => 'author_profile_id', 'show_option_none' => '-- Select --');
	if(!empty($current_author)) $args['selected'] = $current_author;

function gc_author_meta_box_save($post_id, $post) {
	 if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
	 if($post->post_type == 'revision') return;

	 update_post_meta($post_id, '_author_profile_id', strip_tags(trim($_POST['author_profile_id'])));

add_action('save_post', 'gc_author_meta_box_save', 1, 2);

With the inclusion of  custom post types in WordPress 3.0, the number of possible uses for pages as containers for other content only increases. For example, a real estate site could have pages for different neighborhoods that include listings found in the neighborhood. The only stumbling block is that you will need to use a plugin if you want to easily manage the site’s structure.