Skip Navigation

[Resolved] Using a custom function within a cred_save_data hook

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

Problem:

How to pass parameter to PHP function.

Solution:

You can pass parameter to your custom PHP function, for example:

https://toolset.com/forums/topic/using-a-custom-function-within-a-cred_save_data-hook/#post-1825671

Relevant Documentation:

This support ticket is created 4 years, 1 month 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.

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/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by julieP 4 years ago.

Assisted by: Luo Yang.

Author
Posts
#1824883

I have a few forms that execute the same code via a cred_save_data_hook. If the code were to change in the future, rather than having to make multiple changes (and get them all the same), I'm looking at replacing the code with a custom function. I tested this with a very simple example like this:-

My Function:-

if ( ! function_exists( 'my_custom_function' ) ) {
    function my_custom_function() {
        
        $value = '2020';
        update_post_meta($post_id, 'wpcf-my-test-field', $value);
    }
}

My CRED Hook:-

add_action('cred_save_data', 'my_save_data_form_319',10,2);
function my_save_data_form_319($post_id, $form_data) {
    
    if ($form_data['id']==319) {
	
	   my_custom_function();
	}
}

I discovered though that $post_id isn't available to the function so the field isn't updated.

Am I looking to do something that's not possible?

Thanks

#1825671

Hello,

You can pass var $post_id to your custom PHP function, for example:

...
function my_custom_function($post_id) {
...

More help:
hidden link

#1830375

So it DOES need to be passed to the inner function even though it's already been passed to the cred function?

#1831529

Yes, you are right, it needs to be passed to the inner function even though it's already been passed to the cred function

#1833383

thanks for clarifying