Set default date in datepicker of an advanced custom field

I am using Advanced Custom Fields in my WordPress project. In it I have added 2 datepickers as custom fields for pages.

Now the main problem is whenever I add a new page both datepickers by default show up empty, but I want to set the current date of the server as a default, that is whenever I add a new page both the date pickers should have the current date of the server by default in it.

In advance thanks for the help.

Topic advanced-custom-fields datepicker Wordpress

Category Web


Adding a default value to an ACF date picker can be achieved by using a filter:

// Adjust startdate default value
add_filter('acf/load_field/name=startdate', function ($field) {
    $field['default_value'] = date('Ymd');
    return $field;
});

Replace startdate with any field name you want to apply this filter to.

This filter goes into your theme's (or child-theme's) functions.php or into a template file if it makes sense for you.

Source: https://support.advancedcustomfields.com/forums/topic/date-picker-prefilled-to-today/


It depends on the datepicker plugin you are using. Mostly all datepickers will have a default value you can play with. Usualy it is set within the input text type that hosts the datepicker.

<input type="text" class="datepicker" default-value="2013/07/25">

And in wordpress' php you can do :

<input type="text" class="datepicker" default-value="<?php echo date("Y/m/d"); ?>">

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.