John.. or anyone else…?
Is there a way to strip acf_form_head() down to the basics required just for google map? Because it loads all kinds of things like datepickers, etc, etc..?
I managed to make the functionality required, but I find it to be too heavy loading all this stuff. :/
Sure it would be cool to see this in the ACF core – but it’s also nice to keep the core plugin from getting too bloated – plus it would have to be implemented in an optional/backwards compatible way – I would not want clients who have access to HEX color pickers to suddenly be able to add transparency on the next update.
Have you tried https://wordpress.org/plugins/acf-rgba-color-picker/ to fill this need?
Hi GusGF,
I’ve used date formatting like this:
$startmulti = get_sub_field('start_date_1', false, false);
$startmulti = new DateTime($startmulti);
and then in then in my output
$startmulti->format('F j')
This has always worked for me to set the format of the datepicker.
The reason why I posted this originally was because It seems unnecessary to load on the front end for example date picker and timepicker javascript files if the front end is only displaying a text input.
I think the last comment just posted might be echoing this – there is a way to conditionally include scripts on the front end based on what fields are actually being used.
Anyway, I’m not anymore concerned with this but thought I would offer my reasoning. Cheers!
@hube2 , John, sorry for hi-jacking the thread with this off-topic, I’m totally clueless about the issue below, and for a fact I know that if anyone is to help me figure it out, most probably that one will be you.
So I noticed that 3-4 out of 10 times I make some edits in a post (any post) that has an ACF multi-date picker, after the post is re-published, the date picker calendar will lose its localization, until I interact somehow with it. Once I change the month, select an unselected date, or unselect a selected one, the calendar magically gets localized again. And because in my localization settings, Monday is starting the week, even that small detail gets changed as well… And ALL that is not happening every time… As I said, somehow it’s happening 3-4 times out of 10 maybe…
In the video below, until @1:30 I published the post 3-4 times and the calendar didn’t lose its localization. Then suddenly, on @1:30, it lost it two times in a row (where you can see all things I described above). Then, at the end of the video, again it didn’t lose it after one more re-Publish…
Any idea why this may be happening? It’s not super serious, as it’s happening in the backend, but still, I’d like to figure out why!
https://www.youtube.com/watch?v=yKodqaCkrsg
OK, I found it myself! All I needed was ACF Pro, and its Repeater field where I’d set up three more fields: Cinema (a Relationship to Theater), a Time picker, and a Multi-date picker!
The fields were loaded, but not the editor / datepickers…
but after being in contact with the Support…
we found the solution …
Somehow the the difference between 2.5.6 and 2.6.9, wouldnt load the js, if acf_form() is added directly (in my case) in the top of my index.php, but if the get_header hook was used it worked with the newest version
A date can be selected if its visible but if any other text goes over the picker it is not possible to select anything.
Tnx. I was a little afraid it would be done with javascript.
I tried to figure out how (many times) with this tutorial:
https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields
I know how to call javascript to a field but I don’t know what code I should use to add it…
Would it be something like this?
// Customization to datepicker and timepicker
function yl_add_label_class_to_acf_fields() {
?>
<script type="text/javascript">
(function($) {
// Check ACF
if(typeof acf === 'undefined')
return;
// Date picker & Google Maps compatibility
$('.acf-label').addClass('uk-form-label');
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_footer', 'yl_add_label_class_to_acf_fields');
Ah, so I know how I got mine “working”
my web to print system uses ACF form to display the content on the front end of the website (allowing people to move the content around using flexible content).
To make sure the new content is loaded in I added a jquery script that presses the update button when the page is done loading, so it loads twice (bringing in the new content).
So actually, my way wouldn’t work for you.
Your value is saved in the database when choose it on the very last page in wordpress before the front end correct?
You could always change the colour by using classes instead in css.
so have 5 classes that you change the colour of in your header.php for example.
<style>
.class-one {
color: <?php the_field(‘colour_one’, ‘option’); ?>;
}
.class-two {
color: <?php the_field(‘colour_two’, ‘option’); ?>;
}
3-5 etc…
</style>
Then in wordpress options page you can change the HEX code for it which will get pulled through to the class as a HEX value from colour picker.
—
Then in select dropdown you just choose the class name as the value, so you never have to load values dynamically through functions.php
For example:
Well, still not sure why any of the code examples in the ACF forums like this post Expire post to draft or this post Expire Posts on Datepicker field
aren’t working.
Here’s what finally worked for me:
Publish based on datepicker field “publish_coupon_date”
add_action( 'wp', 'publish_coupons_daily' );
function publish_coupons_daily() {
if ( ! wp_next_scheduled( 'publish_coupons' ) ) {
wp_schedule_event( strtotime('07:02:00'), 'daily', 'publish_coupons');//runs at or after 12:02am local time, server time is in UTC
}
}
add_action( 'publish_coupons', 'publish_coupons_function' );
function publish_coupons_function() {
$date_default_timezone_set('America/Los_Angeles'); // ensures the comparison happens based on local timezone, not on server UTC time, so they don't get published too soon
$today = date('Ymd');
$args = array(
'post_type' => array('coupon'), // post types you want to check
'post_status' => 'draft',
'posts_per_page' => -1
);
$posts = get_posts($args);
foreach($posts as $p){
$publishdate = get_field('publish_coupon_date', $p->ID, false, false); // get the raw date from the db. false, false will convert to Ymd while allowing you to use any date format output choice on the field
if (($publishdate > $today) || ($publishdate = $today)) {
//if date is today OR prior to today
$postdata = array(
'ID' => $p->ID,
'post_status' => 'publish'
);
wp_update_post($postdata);
}
}
}
and expiring posts from datepicker field “expire_coupon_date”
add_action( 'wp', 'expire_events_daily' );
function expire_events_daily() {
if ( ! wp_next_scheduled( 'delete_expired_events' ) ) {
wp_schedule_event( strtotime('07:01:00'), 'daily', 'delete_expired_events'); //runs at or after 12:01am local time, server time is in UTC
}
}
add_action( 'delete_expired_events', 'expire_coupon_function' );
function expire_coupon_function() {
date_default_timezone_set('America/Los_Angeles'); // see other note, we set this to ensure it expires on local time, not server time
$today = date('Ymd');
$args = array(
'post_type' => array('coupon'),
'post_status' => 'publish',
'posts_per_page' => -1
);
$posts = get_posts($args);
foreach($posts as $p){
$expiredate = get_field('expire_coupon_date', $p->ID, false, false); // get the raw date from the db. false, false will convert to Ymd while allowing you to use any date format output choice on the field itself
if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
$postdata = array(
'ID' => $p->ID,
'post_status' => 'draft'
);
wp_update_post($postdata);
}
}
}
have fun kiddies.
I honestly do not know why this is happening. Please check this topic, it’s the only thing that I can think of that might be causing the issue https://support.advancedcustomfields.com/forums/topic/date-picker-fields-in-repeaters-gone-wrong-after-wp-5-3-update/
In adddition to hatsumatsu: Writing the timestamps should probably consider the current timezone settings of your WP installation. I was struggling with that for several hours now, checking the timestamps back and forth. Here’s my approach:
/**
* Convert values of named ACF date time pickers from Y-m-d H:i:s to timestamp
* @param string $value unmodified value
* @param int $post_id post ID
* @param object $field field object
* @return string modified value
*/
function acf_save_as_timestamp( $value, $post_id, $field ) {
if( $value ) {
$tz = new DateTimeZone(get_option('timezone_string'));
$date = DateTime::createFromFormat('Y-m-d H:i:s', $value, $tz);
$value = $date->getTimestamp();
}
return $value;
}
add_filter( 'acf/update_value/name=your_field_name', 'acf_save_as_timestamp', 10, 3 );
Or in case you want all date_time_pickers saving timestamps:
add_filter( 'acf/load_value/type=date_time_picker', 'acf_load_as_timestamp', 10, 3 );
Just in case someone needs that
Hi
in addition to my previous post, here a clear video screenshots of what is going on:
http://www.alankabout.com.au/date-and-time-picker.mp4
as you can see, my desktop time was “19/09/2020 11.23 AM” where on the date & time picker it was “19/09/2020 1.23 AM”
date & time picker is 10 hours behind my desktop (my current time in Sydney, Australia).
hope the video helps
thanks
Hi
“With your original code was the default date time not set or was the default date time incorrect?”
– we have lost the old original code
—————————-
for the second part of your reply, i don’t really know.
the developer become blank and could not work out what to do next.
let me clear something first:
– server time and location are correct
what is happen now is this:
– when adding new post, date & time picker display wrong date & time (14 hours behind)
– if i click on “NOW” it display the correct current date & time, but when i click on “publish”, date & time back wrong again (14 hours behind).
all we needs is this:
when adding new post, date & time picker is to display correct (our current) date & time without the needs of click on “NOW”, also to keep (our current) date & time after publishing the post.
thanks
Hi John
it is being a long time since your last reply
we have tried a different date and time picker but unfortunately it did not works as it should.
now back to ACF date & time picker.
we have installed the bellow code but again, we still having a problem with current time.
example:
we create a post at :19/09/2020 12:49 am
but on the date & time picker it is: 18/09/2020 2:49 pm
even so, the developer is completely lost and can not figure out what to do no more.
any help would be much appreciated.
the code:
add_action( ‘acf/save_post’, ‘set_submit_time’ );
function set_submit_time($post_id) {
update_field(‘entry_post_date’, date(‘Y-m-d H:i:s’), $post_id);
}
add_action(‘save_post’, ‘change_content’);
global $post;
global $wpdb;
function change_content($post_id) {
$datefield = get_post_meta($post_id,’entry_post_date’,true);
$post_date = date(“Y-m-d H:i:s”, strtotime($datefield));
$my_post = array();
$my_post[‘ID’] = $post_id;
$my_post[‘post_date’] = $post_date;
remove_action(‘save_post’, ‘change_content’);
wp_update_post( $my_post );
add_action(‘save_post’, ‘change_content’);
}
add_filter(‘acf/load_field/name=entry_post_date’, ‘my_acf_default_date’);
function my_acf_default_date($field) {
$field[‘default_value’] = date(‘Y-m-d H:i:s’);
return $field;
}
OK, I’m going to answer my own question. It took a little time, but you can do it. For now, I’ve edited the core files, but will make my own ACF field to not interfere with the base datetimepicker.
Two things to know:
1. The datetimepicker is an extension of the jQuery UI date picker. It has a known bug that will NOT allow the time to be set in inline mode. However, I have a fix for that.
2. To activate inline mode, you need to target a <div> or <span> as the target for the datetimepicker instead of the <input> tag.
So, step one is to update the render_field
method in the acf_field_date_and_time_picker.php
file and class.
Just to keep to the style of the existing code, I inserted a new DIV using the acf methods here:
$text_input = array(
'class' => 'input',
'value' => $display_value,
);
$inline_calendar = array(
'class' => 'calendar-inline input'
);
// html
?>
<div <?php acf_esc_attr_e( $div ); ?>>
<?php acf_hidden_input( $hidden_input ); ?>
<?php // acf_text_input( $text_input ); ?>
<div <?php acf_esc_attr_e( $inline_calendar ); ?>></div>
</div>
<?php
}
This will insert the div below the text input, and comment out the text input.
Next, update the acf-input.js
file to edit the javascript of the picker.
Note that the datetimepicker is an extension of the date picker. So, around line 5811 there will be a definition for the date_picker and it will declare the $inputText.
$inputText: function(){
return this.$('.calendar-inline');
// return this.$('input[type="text"]');
},
This should now return your .calendar-inline class instead of the <input> tag.
Lastly, a bit further down, in the date_time_picker section (not date_picker section) there is a declaration to add.newDateTimePicker on line 6031. We need to update that to update the calendar with the current time.
// add
acf.newDateTimePicker = function( $input, args ){
// bail ealry if no datepicker library
if( typeof $.timepicker === 'undefined' ) {
return false;
}
// defaults
args = args || {};
// NEW - Remember date/time
$date = args.altField[0].value;
// initialize (This wipes the datetime in inline mode)
$picker = $input.datetimepicker( args );
// update to current datetime with the value 2 lines above.
$picker.datetimepicker('setDate', (new Date($date)) );
// wrap the datepicker (only if it hasn't already been wrapped)
if( $('body > #ui-datepicker-div').exists() ) {
$('body > #ui-datepicker-div').wrap('<div class="acf-ui-datepicker" />');
}
};
We effectively store the datetime before we initialise the datetimepicker and then update it once it’s created.
Now you should have an embedded calendar.
Couple of gotchas:
1. Remember to hard-reload and clear cache to reload the new JS.
2. The acf-input.min.js gets loaded by default, you can test the acf-input.js by updating the /includes/assets.php
file and the enqueue_script location OR just define the constant SCRIPT_DEBUG
to use the non-minified version.
Hi, my date picker field is still not getting the translated value after using the snippet above. I have read this could be a bug after WordPress 5.3?
I am using WordPress 5.5 + ACF PRO 5.9.0 + Polylang Pro 2.8.1 and the following code:
wp_date("F Y", strtotime(get_sub_field('date')));
wp_date is the alternative to date_i18n after WordPress 5.3. date_i18n is giving me the month text only in English too.
Answering my own question… found a snippet online that resolves the issue – credit to whoever provided it!
add_action( 'wp_print_scripts', 'pp_deregister_javascript', 99 );
function pp_deregister_javascript() {
if(!is_admin())
{
wp_dequeue_script('wp-color-picker');
wp_deregister_script( 'jquery-ui-datepicker' );
wp_deregister_script( 'wp-color-picker-js-extra' );
wp_deregister_script( 'wp-color-picker' );
}
}
I think I should FOREACH a part of the array. ? Is that a correct thinking?
Although this does not work, could you help me please? This is the last part in a long journey to develop my own booking system:
// Apply conditions to fields
add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1');
function yl_check_booking_exeptions_session_1( $field ) {
// Retrieve option values. Date value should be like: 20200611 (unformatted)
$rows = get_field('bookings_settings_disabled_exceptions', 'bookings');
if( $rows ) {
foreach ( $rows as $row ) {
$option_date = get_sub_field('bookings_settings_disabled_date', 'bookings', false);
$date = date_i18n('Ymd', strtotime($option_date));
$session = get_sub_field('bookings_settings_disabled_session', 'bookings', false);
$arrays =
array(
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $session, // Compare against option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Datepicker fiels in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against option page value
)
);
}
if ( $session == '1' ) {
$field['conditional_logic'] = array(
$arrays
);
}
}
// Return
return $field;
}
}
Thanks for looking into my code.
But it still only outputs 1 condition (session / date combi) although I have 3 entries in the option page… and the other entries don’t have any effect
This is the condition output I get for for the field:
data-conditions="[[{"field":"field_5ed4181bd63dc","operator":"==","value":"1"},{"field":"field_5ed4178dd63d7","operator":"!=","value":"20200627"}],[]]"
The whole code is this:
// Apply conditions to fields
add_filter('acf/prepare_field/name=bookings_field_time_session_1', 'yl_check_booking_exeptions_session_1');
function yl_check_booking_exeptions_session_1( $field ) {
// Retrieve option values. Date value should be like: 20200611 (unformatted)
if( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
while ( have_rows('bookings_settings_disabled_exceptions', 'bookings') ) {
the_row();
$option_date = get_sub_field('bookings_settings_disabled_date', 'bookings', false);
$date = date_i18n('Ymd', strtotime($option_date));
$session = get_sub_field('bookings_settings_disabled_session', 'bookings', false);
if ( $session == '1' ) {
// Add the condition to the field
$field['conditional_logic'] = array(
// the first nested array holds OR conditions
// there would be a separate nested array for each OR condition
array(
// the second nesting is AND conditions
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $session, // Compare against option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Datepicker fiels in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against option page value
)
),
array(
// this is an example, if there was an OR condition those would go here
)
);
}
}
}
// Return
return $field;
}
Your conditional logic array is not set up correctly
$field['conditional_logic'] = array(
// the first nested array holds OR conditions
// there would be a separate nested array for each OR condition
array(
// the second nesting is AND conditions
array(
'field' => 'field_5ed4181bd63dc', // Time field session 1 in the form
'operator' => '==', // If Value is different, then show the field
'value' => $session, // Compare against option page value
),
array(
'field' => 'field_5ed4178dd63d7', // Datepicker fiels in the form
'operator' => '!=', // If Value is different, then show the field
'value' => $date, // Compare against option page value
)
),
array(
// this is an example, if there was an OR condition those would go here
)
);
I found a solution for this so I thought I would post it here for the next person who comes along.
If you want to extend the existing class, you use this line:
var Field = acf.models.DatePickerField.extend({
It’s used with the date_time_picker
to extend off the date_picker
but with some enhancements.
What I’m doing is writing a ui enhancement to the actual date_picker
(Client wants a button to easily add 30 days to the date), so I’m using that extend snippet and then applying it back to the actual date_picker
. That way my custom code is being run and the date picker popup still works:
(function($) {
var Field = acf.models.DatePickerField.extend({
type: 'date_picker',
// name: 'myprescription_expiration_date',
events: {
'click .date_plus_30': 'onClick'
},
onClick: function( e, $el ){
e.preventDefault();
alert("cat");
}
});
acf.registerFieldType( Field );
})(jQuery);
Totally unrelated to this thread but if you’re wondering what the 'click .date_plus_30': 'onClick'
is referring to, I have already registered a bit of extra ui right after the admin panel date picker with:
add_action('acf/render_field/name=myprescription_expiration_date', array( $this, 'custom_admin_render_field_date_offset'), 15);
public function custom_admin_render_field_date_offset($field)
{
echo '<p><a href="#" class="date_plus_30">Add 30 days</a></p>';
}
Same issue here, but with a ‘time-picker’ field.
I tried changing the field to ‘text’ and I had the same issue – it would not store data. All other fields work OK.
Additional info:
I’m on Custom Fields version 5.8.9
The field that is not saving was the first field I created.
I can write data direct into the database and see it in my post. But if I edit the data and update the post, the data it disappears.
I am using the “Advanced Custom Fields” plugin where i have created a “Date & Time picker” for my posts. I am using Essential Grid plugin to display my posts. So i created a meta field that pulls from my “date & time picker”. But it just displays like this on the front end “2020-04-04 00:00:00” no matter what I do.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.