Creating a content scheduler in WordPress

When creating CMS tools, it is sometimes helpful to allow the automation of content updates. For example, during a sale an ecommerce client may wish to highlight daily deals.

I use ACF (Advanced Custom Fields) to create a lot of WordPress customisations, and using the date picker field makes implementing a simple scheduler quite straight forward.

This example shows how to create a banner scheduler, with each image displaying on different days.

Note: I am using the repeater field so that a flexible number of images can be added. The repeater field is a feature of ACF Pro, but the date picker is part of the free version, so something similar could be achieved using several image fields instead.

Here is the ACF setup:

Within the repeater we create a group of fields to hold the image and the start and end dates for it to be displayed. Note that the format to return the date in can be selected. I have specifically chosen a value that is easy to compare against.

It will look something like this in the CMS when some content is added:

In the template code, we can get the current date and determine which banner to display.

// Compare against today
$today = date('Ymd');
// Check rows exists.
if( have_rows('banners') ):
	// Loop through rows.
	while( have_rows('banners') ) : the_row();
		$banner = get_sub_field('banner');
		$banner_image = $banner['banner_image'];
		$start_date = $banner['start_date'];
		$end_date = $banner['end_date'];
		if ( $today >= $start_date && $today <= $end_date ):
			echo '<img src="'.$banner_image['url'].'" alt="'.$banner_image['alt'].'" />';
			break;
		endif;
	endwhile;
endif;

When an entry has a date within range that image is displayed. Notice that a break is then used. This is to prevent multiple images being displayed if overlapping dates are accidently displayed.

This means that priority is given to images from top to bottom as viewed in the CMS.

What if there was nothing scheduled? It might be the desired result that nothing that would get displayed. However, if your layout or page logic dictates there should always be content in this position, it might be sensible to have a fall-back.

To do this, add an image field into your ACF set up that will be displayed if no scheduled content is found.

// Compare against today
$today = date('Ymd');
$fallback_image = get_field('fallback');
$is_scheduled;
// Check rows exists.
if( have_rows('banners') ):
	// Loop through rows.
	while( have_rows('banners') ) : the_row();
		$banner = get_sub_field('banner');
		$banner_image = $banner['banner_image'];
		$start_date = $banner['start_date'];
		$end_date = $banner['end_date'];
		if ( $today >= $start_date && $today <= $end_date ):
			$is_scheduled = true;
			echo '<img src="'.$banner_image['url'].'" alt="'.$banner_image['alt'].'" />';
			break;
		endif;
	endwhile;
endif;
if ( !$is_scheduled ) {
	echo '<img src="'.$fallback_image['url'].'" alt="'.$fallback_image['alt'].'" />';
}

Adding Time

For even more granular control, it wouldn’t be too tricky to also add times to this concept.

My suggestion would be to create two dropdown boxes, one containing hours and one containing minutes, perhaps at 15 minute intervals. The values could be retrieved and appended to the date to enable a time check to be performed as well.

Leave a Reply

Your email address will not be published. Required fields are marked *