Last active
November 6, 2025 22:20
-
-
Save zackkatz/61ebd0cf211ce83f8baee9ca7e917ed3 to your computer and use it in GitHub Desktop.
GravityView - {entry_slug} Merge Tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Add {entry_slug} Merge Tag for GravityView | |
| * | |
| * This snippet creates a custom merge tag that outputs the GravityView entry slug. | |
| * The entry slug is used in GravityView URLs and can be customized via the | |
| * 'gravityview/entry/slug' filter. | |
| */ | |
| /** | |
| * Register the {entry_slug} merge tag in the merge tag dropdown | |
| * | |
| * @param array $form The current form object | |
| * @return array Modified merge tags list | |
| */ | |
| add_filter( 'gform_custom_merge_tags', function( $merge_tags, $form_id, $fields, $element_id ) { | |
| $merge_tags[] = array( | |
| 'label' => 'Entry Slug', | |
| 'tag' => '{entry_slug}', | |
| ); | |
| return $merge_tags; | |
| }, 10, 4 ); | |
| /** | |
| * Replace {entry_slug} merge tag with the actual entry slug value | |
| * | |
| * @param string $text The text containing merge tags | |
| * @param array $form The current form object | |
| * @param array $entry The current entry object | |
| * @param bool $url_encode Whether to URL encode the value | |
| * @param bool $esc_html Whether to escape HTML | |
| * @param bool $nl2br Whether to convert newlines to <br> tags | |
| * @param string $format The format (html, text, url) | |
| * @return string The text with merge tags replaced | |
| */ | |
| add_filter( 'gform_replace_merge_tags', function( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) { | |
| // Check if our merge tag exists in the text. If not, return the original text. | |
| if ( strpos( $text, '{entry_slug}' ) === false ) { | |
| return $text; | |
| } | |
| // Ensure we have an entry with an ID. If not, return an empty string. | |
| if ( empty( $entry ) || empty( $entry['id'] ) ) { | |
| do_action( 'gravityview_log_error', 'Entry slug merge tag: No entry found', $entry ); | |
| return str_replace( '{entry_slug}', '', $text ); | |
| } | |
| // Get the entry slug using GravityView's Entry object. | |
| if ( ! class_exists( '\GV\GF_Entry' ) ) { | |
| do_action( 'gravityview_log_error', 'Entry slug merge tag: GravityView Entry class not found', $entry ); | |
| return str_replace( '{entry_slug}', '', $text ); | |
| } | |
| // Create a GravityView Entry object from the Gravity Forms entry array. | |
| $gv_entry = \GV\GF_Entry::from_entry( $entry ); | |
| if ( ! $gv_entry ) { | |
| do_action( 'gravityview_log_error', 'Entry slug merge tag: GravityView Entry object not found', $entry ); | |
| return str_replace( '{entry_slug}', '', $text ); | |
| } | |
| // Use the Entry object's get_slug() method | |
| $entry_slug = $gv_entry->get_slug( true ); // Pass true to apply the 'gravityview/entry/slug' filter. | |
| // Handle encoding based on context | |
| if ( $url_encode ) { | |
| $entry_slug = urlencode( $entry_slug ); | |
| } | |
| if ( $esc_html ) { | |
| $entry_slug = esc_html( $entry_slug ); | |
| } | |
| return str_replace( '{entry_slug}', $entry_slug, $text ); | |
| }, 10, 7 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment