Skip Navigation

[Resolved] Where do post_id and form_data come from and how to set

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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 8 replies, has 2 voices.

Last updated by rexJ-2 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#601106

Hi, new in toolset
i have a function my_save_data_action($post_id, $form_data)
how and where to put this shortcode regarding a form? (form id is 1)
And where do post_id and form_data come from?
can i simply write [my_save_data_action] in the form and then it is predefined with
post_id?
Or how to do this?
Regards

#601314

Hi, I'll be glad to help.

i have a function my_save_data_action($post_id, $form_data)
how and where to put this shortcode regarding a form? (form id is 1)

This isn't a shortcode, this is a PHP callback function. The callback will be attached by the "add_action" statement:

add_action('cred_save_data', 'my_save_data_action',10,2);

The proper way to add this code is to edit your child theme's functions.php file and place this PHP code inside the opening and closing PHP tags.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

And where do post_id and form_data come from?
When someone submits a CRED form on your site, the callback function will be triggered and post_id and form_data will be supplied by the form that was submitted. It is automatic.

can i simply write [my_save_data_action] in the form and then it is predefined with
post_id?

No, you cannot use a shortcode like this. You must place the code in your functions.php file.

So in general the process is:
- Build your CRED form
- Place the CRED form on your site using the CRED form shortcode or a CRED form Layout cell
- Place the callback PHP code in your child theme's functions.php file, targeting the correct form ID from previous step
- User visits front-end of site and submits the CRED form
- Post is created
- my_save_data_action function is automatically called, and post_id and form_data are automatically passed into the callback function so you can access that information
- The user is redirected according to the redirection settings in your CRED form, or any redirection overrides you have placed using the CRED API.

#601415

Many thanks,
I have understand downto the function and the added action. As CRED API hooks and so this shortcode/callback function must be triggered somewhere in the form before or after the submit button?
It is these predefined cred_save_data and cred_before_save_data' and so i do not know how to place in form or is it in the view after the form?
Regards
ie:
have placed a function in template functions.php
added

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) 

Also figured out the callback from the use $content and

$wporg_atts = shortcode_atts([
                                    'dateBegin' => 'remember  date',
                                    'datEnd'  => 'remember date',
                                 ], $atts, $tag); 

Some nice features, have tried doing a small function calculation on datetime between two dates using diff and inserted the shortcode with parameters into the form and it is working.

#601518

Just to clarify is it not the php coding in functions but more where to put shortcode actions into forms and layouts regarding CRED, i can't find examples of how o do!

#601584

As CRED API hooks and so this shortcode/callback function must be triggered somewhere in the form before or after the submit button?
The hooks are triggered automatically by the CRED plugin after the submit button is pressed. Nothing else is required.

where to put shortcode actions into forms and layouts regarding CRED
No additional shortcodes are required. The callback PHP functions you created will be automatically triggered during the CRED form submission process.

add_action('cred_save_data', 'my_save_data_action',10,2);

The code above tells WordPress to call the "my_save_data_action" function any time a CRED form is submitted. If you want to call this function only when a specific form is submitted, you must apply a conditional in your code or use the form ID in the hook name. I will show examples of each approach below.

Conditional approach:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {

  // if the form being submitted has ID "12345"
  if ($form_data['id'] == 12345)
  {
    // add your code here, it will be executed only when form "12345" is submitted
  }
}

This approach is more flexible but requires more code. You can apply the same callback function to multiple forms by modifying the conditional.

Form ID in hook name approach:

add_action('cred_save_data_12345', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  // add your code here, it will be executed only when form "12345" is submitted
}

This approach is less flexible, but requires less code. There can be only one my_save_data_action function, and it can only be applied to one form - 12345.

Let me know if I can help clarify this further.

#601719

Hi,
Yes i'm a newbie but i can't figure out how toolset use all these php objects.
i did following function:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data){
        $custom_date ='2017-12-12';
        update_post_meta($post_id, 'wpcf-sumdatofield', $custom_date);
}

i did an test page where i can see fields and this it is wpcf-sumdatofield.
how come that this form field not get overridden with this date from $custom_date?
Regards

#601831

On the other hands is this code working:

add_action('cred_before_save_data_432','save_date');
function save_date(){
	$custom_date ='2017-12-13';
	$_POST['wpcf-sumdatofield'] = $custom_date;
}
 

Probably wrong argument with wp_update_post
Forgot to mention that wpcf-sumdatofield is in a post field group (it is group_id=431) as a part of a form in the Post Forms (with post=432) the form is inserted into a page (post=557)

maybe something like..i don't know..

wp_update_post(array('ID' => $post_id, 'wpcf-sumdatofield' => $custom_date)); 
#601852

Can you send me a screenshot of the post editor screen in wp-admin showing the post that was updated by CRED? Please be sure to show the sumdatofield custom field.

#602131

Think i just use wordpress api and do some coding..

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.