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 4 años, 1 mes. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

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: ,

Este tema contiene 4 respuestas, tiene 2 mensajes.

Última actualización por julieP hace 4 años.

Asistido por: 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:
enlace oculto

#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