Skip Navigation

[Résolu] Using a custom function within a cred_save_data hook

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 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.

This topic contains 4 réponses, has 2 voix.

Last updated by julieP Il y a 8 mois et 3 semaines.

Assigned support staff: Luo Yang.

Auteur
Publications
#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

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

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

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

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