David Stockdale's Scrapcode

Show Block After Set Date Shortcode

Are you looking to display content on your WordPress site dynamically, based on a specific date and time?

Whether it’s announcing an event or closing a vacancy, this tutorial will guide you through the process of adding a shortcode to your WordPress child theme that shows a block only after a predetermined date has passed.

Implementing the Shortcode

Firstly, open your child theme’s “functions.php” file, where we’ll add the necessary PHP code to create our shortcode.

 * Adds shortcode (David Stockdale).
add_shortcode('vacancy_box', 'vacancy_box_shortcode');
 * Creates a Vacancy Box if the application deadline has passed (David Stockdale).
 * Example use:
 * [vacancy_box target_date="08/03/2024 12:00"]
function vacancy_box_shortcode($atts) {
    if (isset($atts['target_date']) && !empty($atts['target_date'])) {
        $current = new DateTime('now');
        $current_date = $current->format('Y-m-d H:i:s');
        $target = DateTime::createFromFormat('d/m/Y G:i', $atts['target_date']);
        if ($target !== false) {
            $target_date = $target->format('Y-m-d H:i:s');
            if ($current_date > $target_date) {
                return '
                    <!-- Your HTML content for the closed vacancy block goes here -->

    return '<p>Error: Invalid or missing target date attribute.</p>';

Using the Shortcode

Now that we’ve added the shortcode to our child theme’s “functions.php” file, let’s see how to use it within the WordPress editor.

[vacancy_box target_date="08/03/2024 12:00"]

Replace “08/03/2024 12:00” with the desired target date and time when you want the content to appear.

The shortcode will automatically display the defined block of content once the specified date and time have passed.

With this shortcode implemented in your WordPress child theme, you have the power to schedule content visibility effortlessly.

Whether it’s for closing vacancies, announcing promotions, or any other time-sensitive content, this technique adds a layer of automation to your WordPress site.

Stay tuned for more WordPress tips and tutorials!

Leave a Reply