Custom JSON for different forms
-
I’m using custom code to change the JSON before sending as you suggested here:
https://wordpress.org/support/topic/change-data-before-send/It’s working as expected!
But I need to send different JSONs to different CF7 forms, how do I do that?
By the way, one suggestion, the plugin itself could have an “Advanced Mode” that would allow me to insert this JSON handler in the Webhook tab itself, how about that?
-
Hey, thanks!
The
ctz_get_data_from_contact_formfilter has two parameters.
Try:add_filter( 'ctz_get_data_from_contact_form', 'example_ctz_get_data_from_contact_form', 10, 2 );
function example_ctz_get_data_from_contact_form( $data, $contact_form ) {
if ( $contact_form->id() === 'your-id' ) {
// DO YOUR STUFF
}
if ( $contact_form->id() === 'your-anoterh-id' ) {
// DO ANOTHER STUFF
}
return $data;
}By the way, one suggestion, the plugin itself could have an “Advanced Mode” that would allow me to insert this JSON handler in the Webhook tab itself, how about that?
Nice suggestion! It’s on the plans, but I never figured a good UI to manage create a JSON and I don’t want to do in a way users will be able to break the entire site (a PHP textarea, for example).
Oh, sorry, I expressed myself badly. I was actually using the “ctz_post_request_args” function because I couldn’t get “ctz_get_data_from_contact_form” to work for me. Is it possible to do something similar with the other function?
Regarding the UI, I think that if you make the “Data sent to Webhook” field editable so that the user can fill in any text and just change the CF7 tags, it would already be an excellent evolution of the plugin:
Example:
I need the data in JSON to be sent within a “payload” and with a “tags” field that will always be filled with “conversion”. I could simply edit the “Data sent to Webhook” field as follows, similar to CF7’s Mail:
{
"payload": {
"name": [your-name],
"email": [your-email],
"tags": [
"Conversion [page-title]"
]
}
}And of course, with some kind of validation that prevents the user from continuing if the JSON is invalid.
I won’t have data manipulation options like mathematical operations, for example, but it already gives me the freedom to create an elaborate JSON adjusted to the API’s needs.
Thank you very much for your attention!
There is no reason to
ctz_get_data_from_contact_formto not work to change form data unless you are using special mail tags (we havectz_get_data_from_special_mail_tagsfor that) as we pass the merge of both to BODY (json encoded).Anyway,
ctz_post_request_argshas 3 arguments (and$contact_formtoo):/**
* Filter: ctz_post_request_args
*
* The 'ctz_post_request_args' filter POST args so developers
* can modify the request args if any service demands a particular header or body.
*
* @since 1.1.0
*/
$result = wp_remote_post( $hook_url, apply_filters( 'ctz_post_request_args', $args, $properties, $contact_form ) );Use the third one.
I’m sorry, but I still can’t get it to work. When I click “send” on the form, it simply doesn’t fire (the email arrives, but the webhook doesn’t). No confirmation/error message appears from CF7.
I wrote the code as follows:
add_filter( 'ctz_post_request_args', 'rd_ctz_post_request_args' );
function rd_ctz_post_request_args( $args, $contact_form ) {
if ( $contact_form->id() === '1810ab0' ) { // Formulário: Download de material
$body = json_decode( $args['body'], true );
$rd_body = array(
'event_type' => 'CONVERSION',
'event_family' => 'CDP',
'payload' => array(
'conversion_identifier' => 'Download: ' . $body['content-download-title'],
'email' => $body['your-email'],
'name' => $body['your-name'],
'empresa' => $body['empresa'],
'cf_cargo_menu_suspenso'=> $body['cargo'],
'legal_bases' => array(
array(
'category' => 'communications',
'type' => 'consent',
'status' => 'granted',
),
),
'tags' => array(
'Baixou material',
),
),
);
$args['body'] = json_encode( $rd_body );
return $args;
}
if ( $contact_form->id() === '72d86bb' ) { // Formulário: Contato
$body = json_decode( $args['body'], true );
$rd_body = array(
'event_type' => 'CONVERSION',
'event_family' => 'CDP',
'payload' => array(
'conversion_identifier' => 'Contato Site',
'email' => $body['your-email'],
'name' => $body['your-name'],
'mobile_phone' => $body['telefone'],
'legal_bases' => array(
array(
'category' => 'communications',
'type' => 'consent',
'status' => 'granted',
),
),
'tags' => array(
'Contato',
),
),
);
$args['body'] = json_encode( $rd_body );
return $args;
}
if ( $contact_form->id() === '6d0e048' ) { // Formulário: Newsletter
$body = json_decode( $args['body'], true );
$rd_body = array(
'event_type' => 'CONVERSION',
'event_family' => 'CDP',
'payload' => array(
'conversion_identifier' => 'Inscrição Newsletter',
'email' => $body['your-email'],
'name' => $body['your-name'],
'legal_bases' => array(
array(
'category' => 'communications',
'type' => 'consent',
'status' => 'granted',
),
),
'tags' => array(
'Newsletter',
),
),
);
$args['body'] = json_encode( $rd_body );
return $args;
}
return $args;
}I had doubts about the form ID, I tried the ID that appears in CF7 and the post ID, both didn’t work: https://snipboard.io/HrxLC4.jpg
My first version of the code worked to send the data to the webhook:
add_filter( 'ctz_post_request_args', 'rd_ctz_post_request_args' );
function rd_ctz_post_request_args( $args ) {
$body = json_decode( $args['body'], true );
$rd_body = array(
'event_type' => 'CONVERSION', // Obrigatório
'event_family' => 'CDP', // Obrigatório
'payload' => array( // Aqui será listado o conteúdo do lead
'conversion_identifier' => 'Download: ' . $body['content-download-title'], // Identificador relevante para a conversão
'email' => $body['your-email'], // Substituído pelo conteúdo do campo "your-email"
'name' => $body['your-name'], // Substituído pelo conteúdo do campo "your-name"
'empresa' => $body['empresa'],
'cf_cargo_menu_suspenso'=> $body['cargo'],
'legal_bases' => array(
array(
'category' => 'communications',
'type' => 'consent',
'status' => 'granted',
),
),
'tags' => array(
'Baixou material',
),
),
);
$args['body'] = json_encode( $rd_body );
return $args;
}I’m not a developer so I’m venturing a bit outside my comfort zone, but I just needed to integrate the forms with our email marketing system. Can you help me identify where I’m going wrong?
Just for the record, I managed to fix the code and it worked:
add_filter( 'ctz_post_request_args', 'rd_ctz_post_request_args', 10, 3 );
function rd_ctz_post_request_args( $args, $properties, $contact_form ) {
if ( $contact_form->id() === 21117 ) { // Formulário: Download de material
$body = json_decode( $args['body'], true );
$rd_body = array(
'event_type' => 'CONVERSION',
'event_family' => 'CDP',
'payload' => array(
'conversion_identifier' => 'Download: ' . $body['content-download-title'],
'email' => $body['your-email'],
'name' => $body['your-name'],
'empresa' => $body['empresa'],
'cf_cargo_menu_suspenso'=> $body['cargo'],
'legal_bases' => array(
array(
'category' => 'communications',
'type' => 'consent',
'status' => 'granted',
),
),
'tags' => array(
'Baixou material',
),
),
);
$args['body'] = json_encode( $rd_body );
return $args;
}
if ( $contact_form->id() === 78 ) { // Formulário: Contato
$body = json_decode( $args['body'], true );
$rd_body = array(
'event_type' => 'CONVERSION',
'event_family' => 'CDP',
'payload' => array(
'conversion_identifier' => 'Contato Site',
'email' => $body['your-email'],
'name' => $body['your-name'],
'mobile_phone' => $body['telefone'],
'legal_bases' => array(
array(
'category' => 'communications',
'type' => 'consent',
'status' => 'granted',
),
),
'tags' => array(
'Contato',
),
),
);
$args['body'] = json_encode( $rd_body );
return $args;
}
if ( $contact_form->id() === 79 ) { // Formulário: Newsletter
$body = json_decode( $args['body'], true );
$rd_body = array(
'event_type' => 'CONVERSION',
'event_family' => 'CDP',
'payload' => array(
'conversion_identifier' => 'Inscrição Newsletter',
'email' => $body['your-email'],
'name' => $body['your-name'],
'legal_bases' => array(
array(
'category' => 'communications',
'type' => 'consent',
'status' => 'granted',
),
),
'tags' => array(
'Newsletter',
),
),
);
$args['body'] = json_encode( $rd_body );
return $args;
}
return $args;
}Thank you very much for your help and the excellent plugin!
Hey! How are you?
In new version we have a setting to change sent date.
I tested it here and it worked great, congratulations! It will really make my life much easier and I will use it a lot.
A small suggestion, I noticed that it doesn’t work with special mail-tags, right? Maybe you can include this in a future version?
Anyways, thank you very much for your help again and the excellent plugin!
I’ll check again.
But I’m pretty sure it works with special mail-tags (you need to add them in the settings before).
The topic ‘Custom JSON for different forms’ is closed to new replies.