How to show the present date and time in WordPress?
In the current world of web development, keeping updated with the present date and time on your WordPress site can increase user experience and keep your content connected. When you’re handling a blog website, an e-commerce website, or a news website, showing the present date and time can deliver your audiences with precious conditions. In this wide guideline, we’ll analyze many techniques to show the present date and time in the WordPress website. You’ll find everything you need to keep your site relevant and expected from code snippets to plugin solutions.
Why show the Present Date and Time?
Before diving into the technical details, let’s consider why you might want to display the present date and time on your WordPress site:
- User Engagement: Knowing the date and time can help users understand how current your content is.
- Time-Sensitive Information: If you’re sharing news, event details, or updates, showing the present time can make your content more relevant.
- Content Personalization: For sites that offer personalized experiences, showing the date and time can increase user reaction.
Method 1: Using WordPress Shortcodes
Shortcodes are an excellent way to add functionality to your WordPress site without needing to write complex code. To display the present date and time using a shortcode, follow these steps:
1. Add a Custom Shortcode
You have to add a custom shortcode to your WordPress theme’s functions.php file. Below is a simple code excerpt to create a shortcode to display the present date and time:
function show_current_datetime() { return date('l, F j, Y g:i A'); } add_shortcode('current_datetime', 'show_current_datetime');
2. Use the Shortcode in Your Posts or Pages
If you will add the shortcode to your functions.php file, you can use it in your posts or pages. Insert the following shortcode where you want the date and time to show:
[current_datetime]
This will output the current date and time in the format “August 4, 2024 at 3:45 pm” (assuming this is the current date and time).
Method 2: Using Widgets to Show Date and Time
If you choose a widget-based solution, WordPress offers some plugins that permit you to display the present date and time in your widget areas. Below is a simple method using a plugin:
1. Install a Date and Time Widget Plugin
- Go to your WordPress dashboard.
- Navigate to Plugins > Add New.
- Explore for “Date and Time Widget.”
- Install the plugin of your choice, and activate it.
- Configure the Widget
- Go to Appearance > Widgets.
- Find the “Date and Time Widget” (or similar) in your list of available widgets.
- Drag and drop your selected widget into your desired section.
- Design the widget settings according to your wishes, such as date format and time zone.
This method permits you to simply add a date and time show to your sidebar, footer, or any other widgetized area of your site.
Method 3: Using PHP Code in Theme Files
For those comfortable with editing theme files, you can add PHP code directly to your WordPress theme to show the present date and time. This method requires editing your theme’s header.php, footer.php, or any other template file where you want the date and time to appear.
1. Edit Your Theme File
Navigate to Appearance > Theme Editor and select the template file you want to edit. Add the following PHP code where you want the date and time to be shown:
<?php echo date('l, F j, Y g:i A'); ?>
2. Save Your Changes
After adding the code, save the changes. The present date and time will now be shown in the location you identified within the theme file.
Method 4: Using JavaScript for Dynamic Updates
If you choose an effective solution where the date and time automatically update without page refreshes, you can use JavaScript. This method involves adding a bit of JavaScript to your site that updates the date and time in real time.
1. Add JavaScript Code
To include JavaScript, you can either add it bluntly to your theme’s footer or use a custom plugin. There’s a basic example:
<span id="current-time"></span> <script> function updateDateTime() { var now = new Date(); var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }; document.getElementById('current-time').innerHTML = now.toLocaleString('en-US', options); } setInterval(updateDateTime, 1000); // Update every second </script>
2. Add an HTML Element for Display
In the WordPress theme file where you want to display the present date and time, add the following HTML element:
html shortcode <div id="present_datetime"></div>
This JavaScript code continuously updates the present date and time and displays it in the present_datetime div.
Method 5: Using a Plugin for Enhanced Functionality
If you need updated attributes or customization, consider using a famous plugin. Here are some famous plugins for showing the current date and time:
- WP Date and Time Shortcode: This plugin provides shortcodes and widgets to show the present date and time in various formats.
- Time and Date Widget: Offers customizable options for showing time and date in various ways.
- Simple Custom Post Order: Incorporates date and time features as part of its post management functionalities.
1. Install and Activate a Plugin
- Go to Plugins > Add New in your WordPress dashboard.
- Explore the plugin you want to use.
- Install the plugin and activate it.
2. Configure the Plugin Settings
- Go to the plugin’s settings page.
- Customize the current date and time format according to your needs.
- Use the delivered shortcodes or widgets to show the present date and time on your site.
Conclusion
Showing the present date and time on your WordPress site can increase user experience and keep your content relevant. Whether you select to use shortcodes, widgets, PHP code, JavaScript, or plugins, there’s a solution that fits your needs. By incorporating one or more of these ways, you can ensure that your visitors always have up-to-date information at their fingertips.
Feel free to experiment with these techniques to find the best solution for your WordPress site. Keeping your site current not only assists engage your visitors but also demonstrates that your content is fresh and relevant. If you have any questions or need further assistance, don’t hesitate to reach out in the comments below!
Thanks for reading our article.
Developer at WPCarePoint
Booking for 30m Free Consultation on Google Meeting: Click here for an appointment today.
contact@wpcarepoint.com
www.wpcarepoint.com