Query Param Merge Tag

Adds {query_param:PARAM} merge tags for pulling values from query/GET params.

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Usage

Example URL: https://domain.example/sample-page/?example=13 Example Merge Tag: {query_param:example}

Code

Filename: gw-query-param-merge-tag.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
 * Gravity Wiz // Gravity Forms // Query Param Merge Tag
 *
 * Adds {query_param:PARAM} merge tags for pulling values from query/GET params.
 *
 * Usage:
 *  Example URL: https://domain.example/sample-page/?example=13
 *  Example Merge Tag: {query_param:example}
 *
 * Plugin Name: Gravity Forms Query Param Merge Tag
 * Plugin URI: https://gravitywiz.com/
 * Description: Adds {query_param:PARAM} merge tags for pulling values from query/GET params.
 * Author: Gravity Wiz
 * Version: 0.1
 * Author URI: https://gravitywiz.com
 */
add_filter( 'gform_replace_merge_tags', function( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
	preg_match_all( '/\{query_param:(.*?)\}/', $text, $dyn_pop_matches, PREG_SET_ORDER );

	if ( ! empty( $dyn_pop_matches ) ) {
		foreach ( $dyn_pop_matches as $match ) {
			$full_tag = $match[0];
			$modifier = $match[1];

			$value = sanitize_text_field( esc_sql( gw_get_get_query_param( $modifier ) ) );

			$text = str_replace( $full_tag, $url_encode ? urlencode( $value ) : $value, $text );
		}
	}

	return $text;
}, 10, 7 );

if ( ! function_exists( 'gw_get_query_param' ) ) :
	/**
	 * Get a query parameter value from $_GET when the key may use
	 * array syntax like filters[53][foo], possibly URL-encoded.
	 *
	 * Examples:
	 *   gw_get_get_query_param( 'filters[53]' );
	 *   gw_get_get_query_param( 'filters%5B53%5D' );
	 *   gw_get_get_query_param( 'filters[53][foo]' );
	 */
	function gw_get_get_query_param( $key ) {

		// Normalize the incoming key using WP helpers.
		$key = wp_unslash( $key );      // In case it came from a request.
		$key = urldecode( $key );       // Decode URL-encoded names like filters%5B53%5D.
		$key = trim( $key, "\"'" );     // Strip surrounding quotes.

		// Work with an unslashed copy of $_GET.
		$get = wp_unslash( $_GET );

		// No array syntax – return directly.
		if ( strpos( $key, '[' ) === false ) {
			return isset( $get[ $key ] ) ? $get[ $key ] : null;
		}

		// Convert "filters[53][foo]" → [ 'filters', '53', 'foo' ].
		$parts = preg_split( '/\[|\]/', $key, -1, PREG_SPLIT_NO_EMPTY );

		$value = $get;

		foreach ( $parts as $part ) {
			if ( ! is_array( $value ) || ! array_key_exists( $part, $value ) ) {
				return null;
			}
			$value = $value[ $part ];
		}

		return $value;
	}
endif;

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.