Redirect unauthenticated users to the login page.

By default, Live Preview will show a login form if a user is not logged in or does not have the permissions to preview a form.

In some situations, these pages can be indexed by search engines. To remedy this, this snippet redirects to wp-login.php instead of showing a login form.

Installation:

  1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/

Code

Filename: gplp-redirect-to-wp-login.php

<?php
/**
 * Gravity Perks // GP Live Preview // Redirect unauthenticated users to the login page.
 * https://gravitywiz.com/documentation/gravity-forms-live-preview/
 *
 * By default, Live Preview will show a login form if a user is not logged in or does
 * not have the permissions to preview a form.
 *
 * In some situations, these pages can be indexed by search engines. To remedy this,
 * this snippet redirects to `wp-login.php` instead of showing a login form.
 *
 * Installation:
 *  1. Install per https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 */
add_action( 'wp', function() {
	if ( ! function_exists( 'gp_live_preview' ) ) {
		return;
	}

	if ( GFCommon::current_user_can_any( 'gravityforms_preview_forms' ) ) {
		return;
	}

	if ( ! gp_live_preview()->is_live_preview() ) {
		return;
	}

	$current_url = 'http' . ( is_ssl() ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

	wp_safe_redirect( wp_login_url( $current_url ) );

	die();
} );

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.