Last active
June 13, 2022 20:04
-
-
Save spivurno/8748689 to your computer and use it in GitHub Desktop.
Gravity Wiz // Gravity Forms Unrequire Required Fields for Speedy Testing
This file contains 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 | |
# Basic Usage | |
# requires that the user be logged in as an administrator and that a 'gwunrequire' parameter be added to the query string | |
# http://youurl.com/your-form-page/?gwunrequire=1 | |
new GWUnrequire(); | |
# Enable for All Users (Including Visitors) | |
# still requires the 'gwunrequire' parameter be added to the query string | |
new GWUnrequire( array( | |
'admins_only' => false | |
) ); | |
# Enable Automatically w/o Requiring the 'gwunrequire' Query Parameter | |
# admin users will never be required to fill out required fields | |
new GWUnrequire( array( | |
'require_query_param' => false | |
) ); |
This file contains 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 | |
/** | |
* Gravity Wiz // Gravity Forms Unrequire Required Fields for Testing | |
* | |
* When bugs pop up on your forms, it can be really annoying to have to fill out all the required fields for every test | |
* submission. This snippet saves you that hassle by unrequiring all required fields so you don't have to fill them out. | |
* | |
* @version 1.0 | |
* @author David Smith <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/speed-up-gravity-forms-testing-unrequire-required-fields/ | |
* @copyright 2013 Gravity Wiz | |
* | |
* @wordpress-plugin | |
* Plugin Name: Gravity Forms Unrequire | |
* Plugin URI: http://gravitywiz.com/speed-up-gravity-forms-testing-unrequire-required-fields/ | |
* Description: When bugs pop up on your forms, it can be really annoying to have to fill out all the required fields for every test submission. This snippet saves you that hassle by unrequiring all required fields so you don't have to fill them out. | |
* Version: 1.0 | |
* Author: Gravity Wiz | |
* Author URI: http://gravitywiz.com/ | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: gwu | |
*/ | |
class GWUnrequire { | |
var $_args = null; | |
public function __construct( $args = array() ) { | |
$this->_args = wp_parse_args( $args, array( | |
'admins_only' => true, | |
'require_query_param' => true | |
) ); | |
add_filter( 'gform_pre_validation', array( $this, 'unrequire_fields' ) ); | |
} | |
function unrequire_fields( $form ) { | |
if( $this->_args['admins_only'] && ! current_user_can( 'activate_plugins' ) ) | |
return $form; | |
if( $this->_args['require_query_param'] && ! isset( $_GET['gwunrequire'] ) ) | |
return $form; | |
foreach( $form['fields'] as &$field ) { | |
$field['isRequired'] = false; | |
} | |
return $form; | |
} | |
} | |
# Basic Usage | |
# requires that the user be logged in as an administrator and that a 'gwunrequire' parameter be added to the query string | |
# http://youurl.com/your-form-page/?gwunrequire=1 | |
new GWUnrequire(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome, thanks! Added it to my Code Snippets library