Skip Navigation

[Resolved] Create custom title

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use the value of a date custom field to create a custom post title with CRED.

Solution: The date field is not a simple text string in the $_POST object, but an array of values in this format:

[wpcf-date-field-slug] => Array
        (
            [display-only] => August 1, 2017
            [datepicker] => 1501545600
            [datetime] => 2017-08-01
            [hour] => 00
            [minute] => 00
            [timestamp] => 1501545600
        )

Use the cred_save_data hook to capture the custom date field information and update the post title as needed using one of these array values:

[php]
$display_date = $_POST['wpcf-date-field-slug']['display-only'];

[php]

Or, you can format your own date using the timestamp value.

Relevant Documentation:
http://php.net/manual/en/function.date.php
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years, 3 months ago. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by charlesR-5 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#557891

Hi,

I'm trying to autogenerate custom post title with my custom fields from a form. I'm able to do it with 2 of my 3 customs fields. The third one, a date field, isn't include in the title. I use this code

//Better titles for forms
// =============================================================================
add_action("cred_save_data", "my_save_data_action",10,2);
function my_save_data_action($post_id, $form_data) {   
    // Enquiry form
    if ( $form_data["id"]==174 ) {
        if (isset($_POST["wpcf-nom"]) && isset($_POST["wpcf-prenom"]) && isset($_POST["wpcf-date-de-naissance"]) ){
        
            //Build the title & slug
            $title = $_POST["wpcf-nom"]." ".$_POST["wpcf-prenom"]." ".$_POST["wpcf-date-de-naissance"];
            $slug = sanitize_title($title);
            // Update the post into the database
            $my_post = array(
                'ID' => $post_id,
                'post_title' => $title,
                'post_name' => $slug
            );
            wp_update_post( $my_post ); 
        } 
    }
}

And the result = Gagnon Jean Array

#557932

Hi, a date field from CRED isn't a simple value in the $_POST object, it's an array of values:

[wpcf-date-field-slug] => Array
        (
            [display-only] => August 1, 2017
            [datepicker] => 1501545600
            [datetime] => 2017-08-01
            [hour] => 00
            [minute] => 00
            [timestamp] => 1501545600
        )

You can access any of these values using array notation like this:

$display_date = $_POST['wpcf-date-field-slug']['display-only'];
$timestamp = $_POST['wpcf-date-field-slug']['timestamp'];

Replace 'wpcf-date-field-slug' with the slug of your date field, using the 'wpcf-' prefix.

If none of the values in this array is what you want to include in the title, you can format a date using the PHP date function and the timestamp value from the array.
hidden link

Keep in mind that the formatted date string will be sanitized for use in a URL, so certain characters will be restricted, like a backslash or other special characters.

#558195

Thank you very much!