Skip Navigation

[Closed] change the _CRED_FORM name by field values

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
- 7:00 – 15:00 7:00 – 15:00 7:00 – 15:00 7:00 – 15:00 7:00 – 15:00 -
- - - - - - -

Supporter timezone: Pacific/Easter (GMT-05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Ilyes 8 months, 3 weeks ago.

Assisted by: Ilyes.

Author
Posts
#2685932

Hi !

I'd like to change the generic name of a form started by _CRED...

In my form I have related post values:
[cred_field field='@company-order.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='true']
[cred_field field='@film-order.parent' class='form-control' output='bootstrap' select_text='--- not set ---']

I want the name of my post created by the form to be
Company- Film - Date

So I add a script:

add_action('cred_save_data', 'func_custom_post_title', 10, 2);
function func_custom_post_title($post_id, $form_data)
{
if ($form_data['id'] == 222) {
$posting_date = date('d m Y');
$film = get_post_meta($post_id, 'wpcf-@film-order.parent', true);
$company = get_post_meta($post_id, 'wpcf-@company-order.parent', true);
$date = $posting_date;
$title = $film . '-' $company . '-' . $date;

$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);
}
}

How can I retrieve in the script the data of the parents @film-order.parent and @company-order.parent ?

Many thanks for your help.

#2685984

Ilyes
Supporter

Timezone: Pacific/Easter (GMT-05:00)

Hello,

Thank you for contacting Toolset support team,

To retrieve the data of the parent fields @film-order.parent and @company-order.parent in your WordPress script, you can use the $_POST superglobal array. Here's how you can modify your script to retrieve those values:


add_action('cred_save_data', 'func_custom_post_title', 10, 2);
function func_custom_post_title($post_id, $form_data)
{
    if ($form_data['id'] == 222) {
        $posting_date = date('d m Y');
        
        // Retrieving values from $_POST
        $film_parent = isset($_POST['wpcf-@film-order.parent']) ? $_POST['wpcf-@film-order.parent'] : '';
        $company_parent = isset($_POST['wpcf-@company-order.parent']) ? $_POST['wpcf-@company-order.parent'] : '';
        
        // Assuming $film_parent and $company_parent are IDs, you can get their titles
        $film = get_the_title($film_parent);
        $company = get_the_title($company_parent);
        
        $date = $posting_date;
        $title = $film . '-' . $company . '-' . $date;

        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

PS : This script is only an example that will retrieve the parent IDs from the $_POST superglobal array, then get their titles using get_the_title() function. Ensure that the values are properly sanitized and validated before using them in your code. Also, make sure that the field names (wpcf-@film-order.parent and wpcf-@company-order.parent) are correct. If they are different, adjust the script accordingly.

Best,

#2685986

Thank you Ilyes,

Unfortunately, the form to create a new post is in a page called "New Order" and sot the title that the script gives me is "New Order - New Order - Date.
It takes twice the title of the page instead the values of the related posts.

Knowing that in my form I have this:
[cred_field field='@company-order.parent' class='form-control' output='bootstrap' select_text='--- not set ---' required='true']
[cred_field field='@film-order.parent' class='form-control' output='bootstrap' select_text='--- not set ---']

Are "wpcf-@film-order.parent" and "wpcf-@company-order.parent" the right syntax ?

#2686406

Hello Ilyes,

Could you please help ?
Many thanks,

Fabrice

#2686492

Ilyes
Supporter

Timezone: Pacific/Easter (GMT-05:00)

Hello Fabrice,

I'm sorry for the delay in response as I had to double check with our team before suggesting a solution,

So the form fields such as field='@company-order.parent' is a field to select a parent post that the child post (which the form will publish) should belong to.

The relationship is a relationship between company (parent) and order (child) post types, and the .parent indicates the field refers to the parent.

So field='@company-order.parent' will have the post ID of the parent company post.

You can get that value from the $_POST object, but your example you shared with the client (that you got from the client) uses the wrong properties. Here's an example of the $_POST object from a test site that has a project < task relationship:

[05-Mar-2024 08:34:40 UTC] _POST: Array
(
    [@project-task_parent] => 130
    [form_submit_1] => Submit
    [_cred_cred_wpnonce_cred_form_132_1] => 04dc53b9f1
    [_cred_cred_prefix_post_id] => 135
    [_cred_cred_prefix_cred_container_id] => 133
    [_cred_cred_prefix_form_id] => 132
    [_cred_cred_prefix_form_count] => 1
)

So you should be able to retrieve the IDs of the parents in your customer's case with $_POST['@company-order.parent'] and $_POST['@film-order.parent']

Best,

The topic ‘[Closed] change the _CRED_FORM name by field values’ is closed to new replies.