Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Last active November 6, 2025 22:20
Show Gist options
  • Select an option

  • Save zackkatz/61ebd0cf211ce83f8baee9ca7e917ed3 to your computer and use it in GitHub Desktop.

Select an option

Save zackkatz/61ebd0cf211ce83f8baee9ca7e917ed3 to your computer and use it in GitHub Desktop.

Revisions

  1. zackkatz revised this gist Nov 6, 2025. 1 changed file with 1 addition and 6 deletions.
    7 changes: 1 addition & 6 deletions entry-slug-merge-tag.php
    Original file line number Diff line number Diff line change
    @@ -5,11 +5,6 @@
    * 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.
    *
    * IMPORTANT: If you're using custom entry slugs (stored in gravityview_unique_id meta),
    * you must also enable the 'gravityview_custom_entry_slug' filter (see below).
    *
    * @see https://www.gravitykit.com/documentation/gravityview-custom-entry-slugs/
    */

    /**
    @@ -63,7 +58,7 @@

    if ( ! $gv_entry ) {
    do_action( 'gravityview_log_error', 'Entry slug merge tag: GravityView Entry object not found', $entry );
    return str_replace( '{entry_slug}', $entry['id'], $text );
    return str_replace( '{entry_slug}', '', $text );
    }

    // Use the Entry object's get_slug() method
  2. zackkatz created this gist Nov 6, 2025.
    81 changes: 81 additions & 0 deletions entry-slug-merge-tag.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    <?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.
    *
    * IMPORTANT: If you're using custom entry slugs (stored in gravityview_unique_id meta),
    * you must also enable the 'gravityview_custom_entry_slug' filter (see below).
    *
    * @see https://www.gravitykit.com/documentation/gravityview-custom-entry-slugs/
    */

    /**
    * 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}', $entry['id'], $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 );