Displaying Today’s Date Anywhere Using Shortcode (Recommended)
source: https://www.wpbeginner.com/wp-tutorials/how-to-display-todays-date-in-wordpress/
For this method, we will create a shortcode and then use it to display the date and time anywhere on our WordPress website.
You can add the following code to your theme’s functions.php file or by using a custom code snippets plugin such as WPCode (recommended):
1
2
3
4
5
6
7
8
9
10
11
12
13
function
wpb_date_today(
$atts
,
$content
= null) {
extract( shortcode_atts(
array
(
'format'
=>
''
),
$atts
) );
if
(
$atts
[
'format'
] ==
''
) {
$date_time
.=
date
(get_option(
'date_format'
));
}
else
{
$date_time
.=
date
(
$atts
[
'format'
]);
}
return
$date_time
;
}
add_shortcode(
'date-today'
,
'wpb_date_today'
);
Hosted with ❤️ by WPCode
1-click Use in WordPress
This code simply creates a shortcode that displays the current date. You can use it by adding this shortcode anywhere on your site:
[date-today]
By default, the shortcode will display the date in the default date format in your WordPress settings.
You can also use your own date format by modifying the shortcode like this:
[date-today format='F j, Y']
Then, it should be something like this on your website.