Support » Developing with WordPress » Custom Field (date) via wp.template – reformat in php

  • Hey guys,

    I need to reformat a date which is stored in a custom field and called via wp.template:

    <?php echo $label_html ?> {{ post_meta_value:<?php echo esc_attr( $key ) ?> }}

    I need to reformat the content of post_meta_value:<?php echo esc_attr( $key ) to “M jS, Y”

    What would be the best way to achieve this? I read different documentations and tried it via underscore.js and with php but the variable is evaluated via js so I can’t grab it.

    I feel sorry if this is a stupid question. Please point me to the correct path then as I don’t know what I could try next.

    Thanks in advance
    Jonas

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    So the date to reformat is the value of $key? Then I don’t understand why you can’t “grab” it before it’s echoed out. Is the idea of M jS, Y format so it can be properly evaluated by JS? What is the current format?

    You can convert most formats to a timestamp with strtotime(). With a timestamp, use date() to return the desired format.
    echo esc_attr( date('M jS, Y', strtotime( $key )));

    Thanks for your quick reply!

    The problem is, that the value I need is in “post_meta_value:$key” – it’s something wp.template syntax which is not very well documentated for my needs …

    $key is just “_event_start_time”…

    Can you provide more context for where you’re trying to make this change? wp.template is for JavaScript templates, not PHP.

    I want to create a custom grid of planned events – for this, I am using Visual Composer Grid Builder.

    The Events are created with WP Events Manager. The meta-key for the date on which the event starts is “_event_start_date” – with this custom field, I can create a grid element.

    The problem is, that the meta-key contains the date in “YYYY-MM-DD” and I need to re-format it.

    So I am trying to edit the function where the meta key field is being evaluated to re-format the corresponding field.

    The problem is, that the grid builder seems to use wp.template – and evaluating the content of the key on frontend by “{{ post_meta_value:$key }}” and I cannot re-format it using date() as I would do it of course.

    Thanks for your time and best regards
    Jonas

    You’ll probably need to ask the Visual Composer developers for help with that, since it’s specific to their plugin and support for commercial plugins and themes isn’t provided here.

    Ah okay – I thought it is PHP / wp.template based so somebody could help here.
    If it is only specific syntax for Visual Composer, I will ask them.

    Thanks for your answer and best regards
    Jonas

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