Site logo

[Support request] Shortcode for Current Month

Home Forums Support [Support request] Shortcode for Current Month

Home Forums Support Shortcode for Current Month

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1695318
    Praveen

    Hi guys,

    Is there a way to display the current month inside a post (with shortcode) without using any additional plugin?

    Its not required in all articles, just in some.

    #1695345
    Elvin
    Staff
    Customer Support

    Hi there,

    There’s no shortcode for this by default.

    But you can reuse something I’ve written before:

    add_shortcode('display_current_date',function($atts){
    	ob_start();
    	$atts = shortcode_atts( array(
    		'timezone' => '',
    		'html_tag' => '',
    		'class' => '',
    		'format' => '',
    		'id' => ''
    	), $atts, 'display_current_date' );
    	
    	date_default_timezone_set($timezone);
    	if( $atts['timezone'] ){
    		date_default_timezone_set($atts['timezone']);
    	}
    	
    	if( empty( $atts['html_tag'] ) ){
    		$html_tag = 'span';	
    	} else{
    		$html_tag = $atts['html_tag'];
    	}
    	
    	if( empty( $atts['id'] ) ){
    		$id = '';
    	} else{
    		$id = 'id="'.$atts['id'].'"';
    	}
    	
    	if( empty( $atts['class'] ) ){
    		$class = '';
    	} else{
    		$class = 'class="'.$atts['class'].'"';
    	}
    	
    	if( empty( $atts['format'] ) ){
    		$format = 'F';
    	} else{
    		$format = $atts['format'];
    	}
    	
    	echo '<'.$html_tag.' '.$id.' '.$class.'>'.date($format).'</'.$html_tag.'>';
    	return ob_get_clean();
    });

    This PHP snippet allows you to use [display_current_date].

    It has a couple of attributes but if you use the default [display_current_date] it will only display the month with <span> HTML tag.

    Here are a few attributes to use.

    Example: making the date display using <h2> element
    [display_current_date html_tag="h2"]

    Example: changing timezone to Asia/Manila.
    [display_current_date timezone="Asia/Manila"]
    List of supported timezones – [display_current_date html_tag=”p” class=”test” id=”test-1″ format=”F j, Y, g:i a” timezone=”Asia/Manila”]

    Example: add class “test”, add id “test-1” and use p HTML tag.
    [display_current_date html_tag="p" class="test" id="test-1"]

    Example: Changing date format to add day, year and timestamp incase you change your mind.
    [display_current_date format="F j, Y, g:i a"]

    Example: using all attributes.
    [display_current_date html_tag="p" class="test" id="test-1" format="F j, Y, g:i a" timezone="Asia/Manila"]

    #1695565
    Praveen

    perfect!

    Is there a chance of this NOT working based on WordPress version updates?

    #1695628
    Elvin
    Staff
    Customer Support

    Is there a chance of this NOT working based on WordPress version updates?

    That’s impossible to answer with 100% certainty. But it’s most likely going to work on WordPress update unless they decide to stop using shortcodesadd_shortcode() and other WordPress functions. (very unlikely)

    #1695640
    Praveen

    Love it! Thanks.

    #1696531
    Elvin
    Staff
    Customer Support

    No problem. 😀

    #1860130
    Tim

    Hi Elvin, awesome shortcode! Thank you very much. Is there a way to translate it to spanish (my wordpress default language)?

    Thanks.

    #1860343
    David
    Staff
    Customer Support

    Hi there,

    try changing this line of code:

    echo '<'.$html_tag.' '.$id.' '.$class.'>'.date($format).'</'.$html_tag.'>';

    to:

    echo '<'.$html_tag.' '.$id.' '.$class.'>'.date_i18n($format, false, false).'</'.$html_tag.'>';

    #1862391
    Tim

    Thank you! I finally used this. That should be ok, right?

    
    function display_date_1() {
        return date_i18n('d/m/Y');
    }
    add_shortcode('date_1', 'display_date_1');
    
    function display_date_2() {
        return date_i18n('d-m-Y');
    }
    add_shortcode('date_2', 'display_date_2');
    
    function display_date_3() {
        return date_i18n('D d.m.Y');
    }
    add_shortcode('date_3', 'display_date_3');
    
    function display_date_4() {
        return "Hoy es ". date_i18n('l') .", " . date_i18n('d') . " de ". date_i18n('F'). " del año " .date_i18n('Y');
    }
    add_shortcode('date_4', 'display_date_4');
    #1862394
    David
    Staff
    Customer Support

    Looks good to me 🙂

    #1862398
    Tim

    Perfect, thanks!

    #1862487
    David
    Staff
    Customer Support

    You’re welcome

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.