Skip Navigation

[Resuelto] Using a custom function within a cred_save_data hook

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 hace 3 años, 5 meses. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Etiquetado: ,

This topic contains 4 respuestas, has 2 mensajes.

Last updated by julieP hace 3 años, 5 meses.

Assisted by: Luo Yang.

Autor
Mensajes
#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

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