Sauter la 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 support ticket is created Il y a 4 années et 2 mois. 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)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par julieP Il y a 4 années et 2 mois.

Assisté par: 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

Hello,

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

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

More help:
lien caché

#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