ACF Pro Date Picker Will Only Go Back 100 Years

Hi @charlie ,

I am using ACF Pro to deal with a non-profit project which is historical in nature. However, ACF’s date picker seems to be limited to +/- 100 years. We have a need to go back at least 200 years on ACF’s date picker. I have looked at the “solution” in this post https://support.advancedcustomfields.com/forums/topic/date-picker-field-starting-from-before-1913/, but it just brings a fatal error for the website.

Do you have any solutions on how I can manipulate the +/- year span through functions.php or any other way?

Thanks,
Christopher

Yeah I’m not sure we have any solutions on hand and it’s partially out of the scope of this forum. What was the fatal error you were getting? Since it allows inputs of dates prior to 1923, you really just need to hack in some other options to that select box as that forum goes into detail on. Have a great weekend!

Hi @charlie,

The fatal errors were caused by the bits in the Secure Note. I appreciate this is likely to be outside normal support!

Many thanks,
Christopher

Use this instead. There was a couple of issues there, I think it was just missing comments in the copying. Have a great day!

<?php

function acfDateRangeYearAdjustment() {

    echo <<<HTML
<script>
window.addEventListener("load", function() {
    // function to merge two Javavascipt objects IE compatible, from prototype.js http://prototypejs.org/doc/latest/language/Object/extend/
    function extend(destination, source) {
      for (var property in source)
        destination[property] = source[property];
      return destination;
    }

    // Acf hook -> Hooks -> Filters -> date_picker_args
    // see https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
    acf.add_filter('date_picker_args', function( args, field ){

      // do something to args

      var custom_args = {
        yearRange:			"-200:+100", // value to change
      };

      args = extend(args, custom_args)

      // return
      return args;

    });
});
</script>
HTML;

}

add_action('admin_enqueue_scripts', 'acfDateRangeYearAdjustment');

Thank you @charlie,

Just to confirm, this goes into functions.php, right?

Many thanks,
Christopher

Yeah that’s good place for it, no problemo happy to help.

Hi @charlie,

I’m afraid that doesn’t seem to work. The <?php at the start of the code threw a fatal error. Removing it lets the website run ok again, but the earliest year is still stuck at 1923 (i.e. -100 years).

Thanks again,
Christopher

Hi Christopher,

It seems that the code has not been loaded, I would suggest you use the admin_footer hook instead of admin_enqueue_scripts. The method needs to be called in the following way.

Current :

add_action('admin_enqueue_scripts', 'acfDateRangeYearAdjustment');

Change it to :

add_action('admin_footer', 'acfDateRangeYearAdjustment');

Hope it helps.
Thanks

Hi @tristup,

Thanks. That didn’t seem to work either for some reason.

2023-02-22_15-17-41

I have over the past day tried other solutions as we, such as:

function my_acf_date_picker_args($args, $field) {
    $args['yearRange'] = '-200:+200';
    return $args;
}

function my_acf_modify_date_picker_args() {
    add_filter('acf/fields/date_time/picker_args', 'my_acf_date_picker_args');
}

add_action('acf/input/admin_footer', 'my_acf_modify_date_picker_args');

None of them work!

Thanks,
Christopher

Hey Christopher,

We’re glad that my colleague was able to help you with your issue.

Cheers!

Sorry the issue is still outstanding. The last reply was meant for another post which was resolved.

Hey Christopher,

The code on this link https://support.advancedcustomfields.com/forums/topic/date-picker-field-starting-from-before-1913/ works for me. As you can see in my screenshot below, I was able to output 1923 below.

image

Please follow the steps below:

First, you need to add this code in your child theme functions.php

function my_admin_enqueue_scripts() {

	wp_enqueue_script( 'date-picker-js', get_template_directory_uri() . '/js/custom_date_picker.js', array(), '1.0.0', true );

}

add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');

Then inside your pro-child theme folder, create a folder called js, and inside the js folder create a file name called custom_date_picker.js. Please add the code below in your custom_date_picker.js file.

// function to merge two Javavascipt objects IE compatible, from prototype.js http://prototypejs.org/doc/latest/language/Object/extend/
function extend(destination, source) {
	for (var property in source)
		destination[property] = source[property];
	return destination;
}

// Acf hook -> Hooks -> Filters -> date_picker_args
// see https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/
acf.add_filter('date_picker_args', function( args, $field ){
	
	// do something to args
	
	var custom_args = {
	  yearRange:			"-200:+100", // value to change
	};
	
	args = extend(args, custom_args)
	
	// return
	return args;
			
});

Hope that helps.

Hi @marc_a,

Thank you. The js folder and content was the step I was missing. However, it is still not working for me. The error seems to be that the enqueue statement in functions.php seems to be looking in the parent theme folder for the script. Please see the screenshots.

My functions.php file with the code you sent:

The file system showing the correct hierarchy you mentioned:
2023-02-24_14-16-56

The JS file with the script you sent me:

The error in Chrome’s Inspector Console - this should read .../themes/pro-child/...?

Thanks,
Christopher

Hey Christopher,

Would you mind sharing your admin credentials so that we can check your setup properly? To do that, please give us the following information in a Secure Note.

  • WordPress Login URL
  • Admin level username and password

You can find the Secure Note button at the bottom of your posts.

Thank you.

Hi @marc_a,

Login credentials in Secure Note.

Thanks,
Christopher

Hey Christopher,

I adjusted to code instead of using get_template_directory_uri(), I changed it to get_stylesheet_directory_uri(). Then it is now working correctly on your date picker.

image

Hope that helps.

Hi @marc_a,

Thank you so much for resolving this. It was one of the last major niggles on this website. I am very grateful!

Christopher

Hey Christopher,

You’re welcome! If you have any other concerns or clarifications regarding our theme features, feel free to open up a new thread.

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.