Add-On for Discord and Gravity Forms
The Add-On for Discord and Gravity Forms plugin has a useful hook for developers to use to further enhance its functionality.
gf_discord_embeds
TYPE ⇢ Filter
This filter allows developers to modify or override the Discord embed payload sent when a Gravity Form entry is submitted. The embed structure follows Discord's embed object format and can be used to customize titles, descriptions, fields, colors, images, and more.
Structure of $embeds
:
PHP
$embeds = [
[
'type' => 'rich',
'color' => hexdec( $color ), // Embed color as decimal
'author' => [
'name' => $site_name,
'url' => home_url()
],
'title' => $title,
'url' => admin_url( 'admin.php?page=gf_entries&view=entry&id='.$form['id'].'&lid='.$entry['id'] ),
'description' => $message,
'fields' => $facts, // Each fact is: ['name' => 'Label', 'value' => 'Value', 'inline' => bool]
'image' => [
'url' => '' // Optional large image
],
'thumbnail' => [
'url' => $image // Site logo or fallback image
],
'footer' => [ // Only if footer is enabled
'text' => $footer_text,
'icon_url' => GFDISC_PLUGIN_DIR.'img/wordpress-logo.png'
],
'timestamp' => 'ISO8601 string', // Optional, shown with footer
]
]
Example Usage
PHP
/**
* Change embed color based on the form ID.
*
* @param array $embeds The current array of Discord embed objects.
* @param array $form The Gravity Forms form object.
* @param array $entry The Gravity Forms entry object.
*
* @return array Modified array of embed objects.
*/
add_filter( 'gf_discord_embeds', function( $embeds, $form, $entry ) {
$form_id = (int) $form['id'];
// Get the current color if set, otherwise default to blue
$current_color = isset( $embeds[0][ 'color' ] ) ? $embeds[0][ 'color' ] : hexdec( '0000FF' );
// Choose a color based on form ID
switch ( $form_id ) {
case 1:
$embeds[0][ 'color' ] = hexdec( '00FF00' ); // Bright green
break;
case 2:
$embeds[0][ 'color' ] = hexdec( 'FF0000' ); // Red
break;
default:
$embeds[0][ 'color' ] = $current_color; // Keep the default color or the existing color
break;
}
return $embeds;
}, 10, 3 );