Support » Plugin: Meta Box - WordPress Custom Fields Framework » Change date format on frontend

  • Just started using Metabox and I’m trying to change the date format on the front end of my website. I’ve tried

    $event_begin = rwmb_meta( 'date_1' );
    echo date( 'd F, Y', strtotime( $date_1 ) );

    However it just inserts the date into the top of my web site instead of changing the date in the correct areas.

    I’ve inserted the date with the following shortcode

    [rwmb_meta meta_key="date_1" type="date"]

    My code for the meta box is

    function date_metabox( $meta_boxes ) {
    	$prefix = '';
    
    	$meta_boxes[] = array(
    		'id' => 'event-date',
    		'title' => esc_html__( 'Event Date', 'Custom Meta Box' ),
    		'post_types' => array( 'post' ),
    		'context' => 'advanced',
    		'priority' => 'default',
    		'autosave' => false,
    		'fields' => array(
    			array(
    				'id' => $prefix . 'date_1',
    				'type' => 'date',
    				'name' => esc_html__( 'Date Picker', 'Custom Meta Box' ),
    				'class' => ‘meta-date’,
    				'inline' => true,
    			),
    		),
    	);
    
    	return $meta_boxes;
    }
    add_filter( 'rwmb_meta_boxes', 'date_metabox' );

    Any help would be greatly appreciated.

    • This topic was modified 3 years, 8 months ago by Janek Krause.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Manny Fleurmond

    (@funkatronic)

    Where is the date supposed to be on your website? Sounds like you need to edit your template or create a child template to place it in the right spot

    Thread Starter Janek Krause

    (@janekkrause)

    Currently have a temporary url set up: http://80e51ec2.ngrok.io/

    But you will see in the upcoming events section I’ve inserted a meta shortcode for the event date.

    Using the Genesis Sample child template, I’ve already tried adjusting the functions.php with

    $event_begin = rwmb_meta( 'date_1' );
    echo date( 'd F, Y', strtotime( $date_1 ) );

    Which just randomly adds the date to the header of the page.

    Plugin Contributor Manny Fleurmond

    (@funkatronic)

    Its doing that because you aren’t adding it to the right place. where are you placing the shortcode?

    Thread Starter Janek Krause

    (@janekkrause)

    Assume you are talking about the shortcode? The shortcode is placed in the Genesis Featured Posts widget.

    The section I’ve placed the shortcode in is labeled ‘show post info’. I’m also using another Metabox shortcode for the location, which is working fine.

    Screenshot: http://i.imgur.com/kBb2bci.png

    Plugin Author Anh Tran

    (@rilwis)

    Hi Jane,

    Can I confirm something? Where did you call this code?

    $event_begin = rwmb_meta( 'date_1' );
    echo date( 'd F, Y', strtotime( $date_1 ) );

    Do you put it in the input of post meta in the screenshot above?

    If so, the code will actually runs when Genesis parse that content and it might go to some place unexpectedly.

    FYI, we’ve just created a ticket to add format to the output of date field in the rwmb_meta shortcode.

    Thread Starter Janek Krause

    (@janekkrause)

    Hey Tran,

    The code was placed at the end of the child theme’s function.php file. I wasn’t sure where else to put it.

    Sounds great that you are looking at implementing the date format change of shortcodes in future releases.

    Plugin Author Anh Tran

    (@rilwis)

    Oh, you shouldn’t put the code in the functions.php. Putting there makes the code runs before any page template is loaded, and thus it appears in the header, above all the content.

    In case of Genesis, you need to look at its hook system to know which hook is used in a specific place to show the date. I’m not a pro on Genesis and can’t help on this.

    This is a sample code on showing the value at the beginning of post content using the_content filter. It’s a general WP filter, so will work:

    add_filter( 'the_content', 'prefix_output_date' );
    function prefix_output_date( $content ) {
        if ( !is_single() ) {
            return $content;
        }
        $event_begin = rwmb_meta( 'date_1' );
        return date( 'd F, Y', strtotime( $date_1 ) ) . $content;
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Change date format on frontend’ is closed to new replies.