Skip to content

Instantly share code, notes, and snippets.

@itsViney
Last active March 26, 2022 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsViney/0164908054ba969146335623aa7f24bd to your computer and use it in GitHub Desktop.
Save itsViney/0164908054ba969146335623aa7f24bd to your computer and use it in GitHub Desktop.
Wordpress shortcode based on Advanced Custom Fields' standard shortcode that adds the ability to specify a format for date fields.
/* Enhanced ACF date shortcode. Return format must be set to Ymd in ACF for this to work
Example: [acf_date field="my_date_field" post_id="1000" date_format="d F Y"]
*/
function acf_date_shortcode($atts)
{
extract(shortcode_atts(array(
'field' => '',
'post_id' => false,
'format_value' => true,
'date_format' => ''
), $atts));
$acf_date = get_field($field, $post_id, $format_value);
$value = date_i18n($date_format, strtotime($acf_date));
return $value;
}
add_shortcode('acf_date', 'acf_date_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment