Navigation überspringen

[Gelöst] Add Default value for date field in admin

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Add Default value for date field in admin

Solution:
User was using custom JS and the fix was provided within that custom JS code.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/filling-custom-fields-using-js/#post-1962755

Relevant Documentation:

This support ticket is created vor 3 Jahren, 10 Monaten. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Dieses Thema enthält 9 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von zeljko-turkaljS vor 3 Jahren, 10 Monaten.

Assistiert von: Minesh.

Author
Artikel
#1962347

I have created 'posting' CPT and (among others) 'Sold Expiration Date' custom field of type Date in it.

I want to programmatically populate 'Sold Expiration Date' using javascript when creating new or editing posts of that CPT in WP backend. I don't use CRED. I'm aware of the approach explained in this https://toolset.com/forums/topic/how-to-set-default-value-for-post-field-field-type-date thread, but it doesn't fit my needs, because I have to modify this date based on the value of other custom fields, so I need to use JS.

This is what have I done so far: I'm hooking on admin_enqueue_scripts, then check if I'm editing or posting new 'posting' and then enqueue this JS:

jQuery( document ).ready( function( $ ) {
// date format: April 20, 2021
var dateOptions = { year: 'numeric', month: 'long', day: 'numeric' };

const currentTimestamp = Date.now();
const expirationTimestamp = new Date(currentTimestamp + 2629743000 * 12); // now + 12 months

$('input[name="wpcf[sold-expiration-date][display-only]"]').val( expirationTimestamp.toLocaleString('en-US', dateOptions) );
$('input[name="wpcf[sold-expiration-date][datepicker]"]').val( Date.parse(expirationTimestamp) );
} );

It works as expected, when saving the post wpcf-sold-expiration-date option is populated with the timestamp, but the field 'Sold Expiration Date' remains empty, so I obviously missed some step to properly submit the field. Can you tell me what am I missing?

Best regards,
Petar

#1962645

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please try to add the following line of the code at last within your code:

$( 'input[name="wpcf[sold-expiration-date][display-only]"]').datepicker({ defaultDate: expirationTimestamp.toLocaleString('en-US', dateOptions) });

I hope the above solution will help you to resolve your issue.

#1962683

Hi Minesh,

unfortunately, it didn't help. This line even removes 'Select date' image next to input.

#1962689

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

The thing is that its hard to replace the existing date field.

Can you please share problem URL and admin access details and tell me where exactly you added the code you shared?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1962735

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

I'm not sure but please check the following screenshot: versteckter Link

I can see date field is filled with date value and date picker icon is also displayed.

#1962741

Yes, that's true. Now try to save that post (you'll have to fill up some mandatory fields) and after reload, date will be empty 🙁

#1962755

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've created the following test post: versteckter Link

And once I publish that, I can see the date field value remains populated now.

I've adjusted the JS code as given under:

jQuery( document ).ready( function( $ ) {
    // $() will work as an alias for jQuery() inside of this function

    // hide Sold Expiration Date
    // $('div[data-wpt-id="wpcf-sold-expiration-date"]').hide();

    // date format: April 20, 2021

    var dateOptions = { year: 'numeric', month: 'long', day: 'numeric' };
	
	

    const currentTimestamp = Date.now();
    const expirationTimestamp = new Date(currentTimestamp + 2629743000 * 12);  // now + 12 months


var urlParams = new URLSearchParams(window.location.search); //get all parameters
var posttype = urlParams.get('post_type'); //extract the foo parameter - this will return NULL if foo isn't a parameter

	if(posttype) { 

			$('input[name="wpcf[sold-expiration-date][display-only]"]').val( expirationTimestamp.toLocaleString('en-US', dateOptions) );
			ts = Date.parse(expirationTimestamp)/1000;
			$('input[name="wpcf[sold-expiration-date][datepicker]"]').val( ts );
	}
    //$('input[name="wpcf[sold-expiration-date][datepicker]"]').val( Date.parse(expirationTimestamp) );
	
    // $('input[name="wpcf[sold-expiration-date][display-only]"]').datepicker({ defaultDate: expirationTimestamp.toLocaleString('en-US', dateOptions) });

} );

#1963141

It works 🙂

So, you are basicaly executing the code for filling the date field only when the new post is being created, have I got that right?

Thank you very much for the quick assistence!

#1963679

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

So, you are basicaly executing the code for filling the date field only when the new post is being created, have I got that right?
==>
Yes, that's correct 🙂

#1963691

My issue is resolved now. Thank you!