Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Last active December 29, 2024 22:21
Show Gist options
  • Save mariovalney/3f6f4ea6f864239c9f7b327e75b201e9 to your computer and use it in GitHub Desktop.
Save mariovalney/3f6f4ea6f864239c9f7b327e75b201e9 to your computer and use it in GitHub Desktop.
Format data to send a webhook to discord
<?php
/**
*
* Plugin Name: CF7 to Webhook - Discord Integration
* Description: Format data to send a webhook to discord
* Version: 1.0.0
* Author: Mário Valney
* Author URI: https://mariovalney.com
* Text Domain: cf7-to-webhook-discord-integration
*
*/
add_filter( 'ctz_post_request_args', 'discord_ctz_post_request_args' );
function discord_ctz_post_request_args( $args ) {
if ( empty( $args['body'] ) ) {
return $args;
}
$body = json_decode( $args['body'], true );
// Validate data. Add your own validation to change only this form.
if ( empty( $body ) || empty( $body['your-name'] ) || empty( $body['your-discordtag'] ) || empty( $body['your-email'] ) || empty( $body['your-message'] ) ) {
return $args;
}
$discord_body = array(
'content' => 'New message from ' . $body['your-name'] . ' on your website.',
'embeds' => array(
"title" => "Contactform",
"description" => sprintf( "**Discord-Tag:** %s\n**E-Mail:** %s\n\n**Message**\n%s", $body['your-discordtag'], $body['your-email'], $body['your-message'] ),
"color" => 9291330,
"author" => array(
"name" => $body['your-name'],
"icon_url" => "https://ennorath.org/wp-content/uploads/2020/03/mail.png",
),
),
);
$args['body'] = json_encode( $discord_body );
return $args;
}
@iamkiem
Copy link

iamkiem commented Apr 1, 2024

Hey! @mariovalney

I've been trying for the last 5 days to create succesfully a webhook. I've used Zapier but it seems to be a free 14 day trial. I can't pay for the usage.

I'm trying to send a notification to Discord after someone uses my CF7 form. Since it will be more than 2000 words, webhook doesnt work.

I'm trying to modify the "Data sent to Webhook", so I can add only the "notification" sent to my discord channel, but i can't modify it.

I'm really not a tech/programming guy, so i would be very very grateful if you could help me.

This is my form code:

[hidden your-subject id:your-subject class:your-subject "Postulación Ilegales"]

Nombre de la Organización
[text* your-subject autocomplete:name]

Discord del Jefe
[text* Discord autocomplete:name]

Historia de la Organización
[textarea* HistoriadelaOrganizacion]

Ubicación de la Sede (Imagen del Lugar y Ubicación en el Mapa)
[file* Ubicacion1 limit:5mb filetypes:jpg|png] [file* Ubicacion2 limit:5mb filetypes:jpg|png]

Información de los Miembros (Nombre IC y OOC, Edad IC y OOC, Discord, Historia de los Jefes)
[textarea* InfoMiembros]

Vestimenta (Ejemplo de Vestimenta y Colores Predominantes)
[file* vestimenta1 limit:5mb filetypes:jpg|png] [file* vestimenta2 limit:5mb filetypes:jpg|png]

Actividades que Realizarán
[textarea* Actividades]

Normativa IC y OOC de la Organización
[textarea* Normativa]

¿Por qué deberíamos elegirlos?
[textarea* PorqueElegirlos]

[submit "Enviar"]

Hopefully you can come up whit a solution, i dont know what else to do.

@mariovalney
Copy link
Author

Hey. Check new version.
We have custom body now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment