Allow Top-Window Navigation

Allow user-clicked links in selected popup content to navigate the current page when the links use target=”_top” or target=”_parent”. Popups not selected below retain the default sandbox.

Instructions

  1. Install this snippet by following the steps here: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/

  2. Update the configuration in the snippet:

    • Set $enable_globally to true to enable top-window navigation for every popup.
    • Otherwise, add the popup feed IDs that should allow top-window navigation to $enabled_feed_ids.

Code

Filename: gpp-allow-top-window-navigation.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
<?php
/**
 * Gravity Perks // Popups // Allow Top-Window Navigation
 * https://gravitywiz.com/documentation/gravity-forms-popups/
 *
 * Allow user-clicked links in selected popup content to navigate the current page when the links
 * use target="_top" or target="_parent". Popups not selected below retain the default sandbox.
 *
 * Instructions:
 *
 * 1. Install this snippet by following the steps here:
 *    https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *
 * 2. Update the configuration in the snippet:
 *    - Set $enable_globally to true to enable top-window navigation for every popup.
 *    - Otherwise, add the popup feed IDs that should allow top-window navigation to $enabled_feed_ids.
 */
add_filter( 'gpp_popup_config', function( $config, $feed ) {
	/**
	 * Set to true to enable this for every popup.
	 */
	$enable_globally = false;

	/**
	 * Popup feed IDs that should allow top-window navigation.
	 *
	 * These are popup feed IDs, not Gravity Forms form IDs.
	 */
	$enabled_feed_ids = array(
		123,
		456,
	);

	$feed_id = (int) ( $feed['id'] ?? 0 );

	if ( ! $enable_globally && ! in_array( $feed_id, $enabled_feed_ids, true ) ) {
		return $config;
	}

	$permission = 'allow-top-navigation-by-user-activation';
	$sandbox    = preg_split(
		'/\s+/',
		trim( (string) ( $config['iframeSandbox'] ?? '' ) )
	) ?: array();

	if ( ! in_array( $permission, $sandbox, true ) ) {
		$sandbox[] = $permission;
	}

	$config['iframeSandbox'] = implode( ' ', array_filter( $sandbox ) );

	return $config;
}, 10, 2 );

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.