Skip to main content
Fixes

How to make a WordPress current date shortcode

By 09/05/2023May 23rd, 2023No Comments

WP current date shortcode

Instead of using a plugin like wp time and date, we much prefer to use code to solve these little problems. Code is more efficient and provides a more secure way to customise your website.

In the world of WordPress, shortcodes are a convenient way to add complex functions or content to your pages or posts. A current date shortcode is a useful tool for websites that need to display the current date, whether it’s for an event, a product launch, or just to keep your content up-to-date.

In this blog, we’ll show you how to add a current date shortcode in WordPress and provide tips for using it effectively.

Prerequisites

Before we dive into the steps for adding a current date shortcode, there are a few prerequisites you’ll need to be aware of. Firstly, you’ll need a basic understanding of WordPress functions and PHP. Additionally, your WordPress site will need to have the following:

  • The ability to add custom code to your theme
  • An active child theme (if you’re using a parent theme)

Step-by-step guide

Adding a current date shortcode in WordPress is a straightforward process that can be broken down into two main steps: creating the shortcode function and using the shortcode in your content.

Now, we’ll add this code to our theme’s functions.php file. Here’s how:

  • Log in to your WordPress dashboard
  • Navigate to Appearance > Theme Editor
  • Find the functions.php file in the right-hand file list and click on it to open it
  • Add the following code to the bottom of the file:
function display_current_date() {    return date('jS F Y');}add_shortcode('current_date', 'display_current_date');  
  1. Using the shortcode in your content: We can use it in our pages or posts now that we have created the shortcode function. Here’s how:
  • Navigate to the page or post where you want to add the shortcode
  • Place your cursor where you want the date to appear
  • Type the shortcode, including the square brackets: [current_date]
  • Update the page or post and check to see if the current date is displayed as expected

Troubleshooting common issues

Despite its simplicity, adding a current date shortcode can sometimes go wrong. Here are a few common issues and their solutions:

  • The date is not displayed: Ensure that you have added the shortcode function to your functions.php file and used the correct shortcode in your content. If you’re still having trouble, check your PHP code for syntax errors or your website’s error log for more information.
  • The date format is incorrect: You’ll need to modify the PHP code in your shortcode function to change the date format. The date() function takes a format string as its first argument, and you can use different format codes to customize the date and time display.

Conclusion

Adding a current date shortcode in WordPress is a quick and easy way to keep your content up-to-date. Whether you’re planning an event, launching a new product, or need to keep your content fresh, a current date shortcode can help you achieve your goals.

We hope this guide has been helpful and encourage you to experiment with different shortcode functions.

Need a price for a new website instantly

Try our website cost calculator for free and get a price emailed to you.

Leave a Reply