Current Date Shortcode

Below, you’ll find a detailed guide on how to add the Shortcode for Current Date Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Shortcode for Current Date Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Shortcode for Current Date Plugin and the shortcodes it provides:

Plugin Icon
Shortcode for Current Date

"Shortcode for Current Date is a valuable WordPress plugin that allows users to automatically insert the current date into their posts or pages using a simple shortcode."

★★★★★ (114) Active Installs: 20000+ Tested with: 6.3.2 PHP Version: false
Included Shortcodes:
  • [current_date]

Current Date [current_date] Shortcode

The ‘current_date’ shortcode from Shortcode for Current Date plugin allows you to display the current date in your posts. It accepts three attributes: ‘format’, ‘size’, and ‘color’. By default, the date format is ‘jS F Y’, but you can customize it using the ‘format’ attribute. The ‘size’ and ‘color’ attributes allow you to control the appearance of the date. The shortcode uses WordPress time, not server time.

Shortcode: [current_date]

Parameters

Here is a list of all possible current_date shortcode parameters and attributes:

  • format – determines the format of the displayed date
  • size – sets the font size of the displayed date
  • color – sets the color of the displayed date text

Examples and Usage

Basic example – Utilize the shortcode to display the current date with a default format.

[current_date /]

Advanced examples

Display the current date with a specified format. The format is defined using PHP date format symbols.

[current_date format="d-m-Y" /]

Use the shortcode to display the current date with a specified format and size. The size is defined in pixels.

[current_date format="d-m-Y" size="16px" /]

Display the current date with a specified format, size, and color. The color is defined using hexadecimal color codes.

[current_date format="d-m-Y" size="16px" color="#ff0000" /]

Use the shortcode to display the current day of the year (0-365). This example also includes a specified size and color.

[current_date format="z" size="16px" color="#ff0000" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [current_date] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'current_date', array( $this, 'sfcd_date_shortcode' ) );

Shortcode PHP function:

function sfcd_date_shortcode( $atts ) {

			/**
			 * Shortcode attributes.
			 *
			 * @since 1.0
			 */
			$atts = shortcode_atts(
				array(
					'format' => '',
					'size'   => '',
					'color'  => ''
				), $atts
			);

			if ( ! empty( $atts['format'] ) ) {
				$dateFormat = sanitize_text_field( $atts['format'] );
			} else {
				$dateFormat = 'jS F Y';
			}

			/* Changed [return date( $dateFormat );]
			 * to the following line in order to retrieve
			 * WP time as opposed to server time.
			 *
			 * @author UN_Rick
			 */
			if ( ! empty( $atts['size'] ) || ! empty( $atts['color'] ) ) {
				$size_attr  = esc_attr( $atts['size'] );
				$color_attr = esc_attr( $atts['color'] );

				if ( $dateFormat == 'z' ) {
					return "<p class='sfcd-date' style='font-size:" . $size_attr . "; color: " . $color_attr . ";'>" . ( date_i18n( $dateFormat ) + 1 ) . "</p>";
				} else {
					return "<p class='sfcd-date' style='font-size:" . $size_attr . "; color: " . $color_attr . ";'>" . date_i18n( $dateFormat ) . "</p>";
				}
			} else {
				if ( $dateFormat == 'z' ) {
					return date_i18n( $dateFormat ) + 1;
				} else {
					return date_i18n( $dateFormat );
				}
			}
		}

Code file location:

shortcode-for-current-date/shortcode-for-current-date/shortcode-for-current-date.php

Conclusion

Now that you’ve learned how to embed the Shortcode for Current Date Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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