The gform_slack_username filter allows you to change the username that sends the message to Slack before it’s sent.
Usage
To apply the filter to all forms:
add_filter('gform_slack_username', 'your_function_name', 10, 4);
To target a specific form, append the form ID to the hook name:
add_filter('gform_slack_username_10', 'your_function_name', 10, 4);
Parameters
- $username (string) – The current username being used for the Slack message. Default is: Gravity Forms.
- $feed (Feed Object) – The current feed object.
- $entry (Entry Object) – The current entry object.
- $form (Form Object) – The current form object.
More information
See Gravity Forms Docs: gform_slack_username
Examples
Change the username for a specific form
This example changes the username for form ID 4 to “Support Bot”:
add_filter('gform_slack_username_4', 'change_slack_username', 10, 4); function change_slack_username($username, $feed, $entry, $form) { $username = 'Support Bot'; return $username; }
Change the username based on entry field value
This example changes the username based on the value of field ID 1 in the form entry:
add_filter('gform_slack_username', 'dynamic_slack_username', 10, 4); function dynamic_slack_username($username, $feed, $entry, $form) { $username = $entry['1']; return $username; }
Change the username based on form title
This example changes the username based on the form title:
add_filter('gform_slack_username', 'title_based_slack_username', 10, 4); function title_based_slack_username($username, $feed, $entry, $form) { $username = $form['title']; return $username; }
Change the username based on user role
This example changes the username based on the user role who submitted the form:
add_filter('gform_slack_username', 'role_based_slack_username', 10, 4); function role_based_slack_username($username, $feed, $entry, $form) { $user = get_userdata($entry['created_by']); $user_role = array_shift($user->roles); $username = ucfirst($user_role) . ' User'; return $username; }
Add custom prefix to the username
This example adds a custom prefix “GF-” to the username:
add_filter('gform_slack_username', 'prefix_slack_username', 10, 4); function prefix_slack_username($username, $feed, $entry, $form) { $username = 'GF-' . $username; return $username; }