Skip Navigation

[Gelöst] Default value for generic field

This support ticket is created vor 2 Jahre, 10 Monate. There's a good chance that you are reading advice that it now obsolete.

Dies ist das technische Support-Forum für Toolset – eine Suite von Plugins zur Entwicklung von WordPress-Websites ohne PHP-Pogrammierung.

Jeder kann in diesem Forum lesen, aber nur Toolset-Kunden können darin Beiträge veröffentlichen. Der Toolset-Support arbeitet 6 Tage pro Woche, 19 Stunden pro Tag.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 7 Antworten, has 2 Stimmen.

Last updated by martinB-16 vor 2 Jahre, 10 Monate.

Assigned support staff: Waqar.

Author
Artikel
#1708121

I would like to set the default value of a generic field (date) with the post date in a form
How does it work?

#1708407

Waqar
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Martin,

Thank you for contacting us and I'd be happy to assist.

From your message, I understand that you have a date type generic field in your post form and you'd like to prefill it with a default value that is equal to today's date.

If that is correct, you'll need a custom shortcode, that returns the UNIX timestamp of today's date at midnight:


function get_current_date_timestamp_func() {
	$timestamp = strtotime('today midnight');
	return $timestamp;
}
add_shortcode("get_current_date_timestamp", "get_current_date_timestamp_func");

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through active theme's "functions.php" file.

Next, you can include this custom shortcode as a default value in the code of the generic field, like this:


<label>Post Date</label>
[cred_generic_field type='date' field='post-date']
{
"default":[get_current_date_timestamp],
"required":1,
"validate_format":1
}
[/cred_generic_field]

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1709155

Hello,
no, they didn't understand that correctly. I don't want to set the current date, but the original post date as the default.

#1710303

Waqar
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back and I apologize for the confusion.

As you've mentioned "original post date", I'll assume that this form is a for editing an existing post and not to create a new one?

I'll be able to help you more efficiently and with precise steps, if you could share the link to a page where this form can be seen, along with temporary admin login details.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

#1715963

Waqar
Supporter

Languages: Englisch (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for waiting, while I performed some further testing on my website.

Unfortunately, the password for your website's admin access didn't work, but I managed to connect a date type generic field in a post edit form, to post date, using these steps:

1. In the edit form, I used the "wpv-post-date" shortcode to set the default value of a generic date type field, equal to the current post's date:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-date )


<div class="form-group">
<label>Post Date</label>
[cred_generic_field type='date' field='post-date']
{
"required":0,
"validate_format":1,
"default":[wpv-post-date format="U"]
}
[/cred_generic_field]
</div>

As a result, when the edit form loads, it shows the current post's published date, automatically filled in that date type generic field.

2. Next, I added a custom function attached to the "cred_save_data" hook, so that the date selected in that generic field can be updated, as the current post's published date, when the form is submitted:
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data )


add_action('cred_save_data', 'set_post_date_to', 100, 3);
function set_post_date_to($post_id, $form_data) {
  if( $form_data['id'] == 12345 ) {
    $newDate = $_POST['post-date']['datetime'] . ' 00:00:00';
    $my_post = array(
      'ID'           => $post_id,
      'post_date' => $newDate,
      'post_date_gmt' => $newDate,
    );
 
    // Update the post into the database
    wp_update_post( $my_post );
  }
}

Note: you'll replace "12345" with your actual form's ID.

#1716541
in frontend.jpg
in Template-Editor.jpg
code in post-form.jpg

The code for the default value does not work.
The date field remains empty.

See hardcopys

#1716585

I found the problem. WP doesn't like the date 1970-01-01. It works on another date.

#1716607

My issue is resolved now. Thank you!

Das Forum „Toolset Professioneller Support“ ist für neue Themen und Antworten geschlossen.