wp_reset_vars()
Resets global variables based on $_GET and $_POST
This function resets global variables based on the names passed in the $vars array to the value of $_POST[$var] or $_GET[$var] or '' if neither is defined.
No Hooks.
Return
null
. Nothing.
Usage
wp_reset_vars( $vars );
- $vars(array) (required)
- An array of globals to reset.
Examples
#1 Demonstration
Suppose we do not know beforehand where in $_GET or $_POST the variable foo
with value bar
is passed to the request. We need to set this variable to global if such a variable is passed in the query:
wp_reset_vars( array('foo') ); /* As a result: If a variable was passed to $_POST['foo'] then global $foo will be equal to 'bar' If a variable was passed to $_GET['foo'] then global $foo will be equal to 'bar' If no variables were passed, global $foo will be equal to '' */
In the same way, you can specify several variables at once:
wp_reset_vars( array('foo', 'foo2') );
Changelog
Since 2.0.0 | Introduced. |
wp_reset_vars() wp reset vars code WP 6.1.1
function wp_reset_vars( $vars ) { foreach ( $vars as $var ) { if ( empty( $_POST[ $var ] ) ) { if ( empty( $_GET[ $var ] ) ) { $GLOBALS[ $var ] = ''; } else { $GLOBALS[ $var ] = $_GET[ $var ]; } } else { $GLOBALS[ $var ] = $_POST[ $var ]; } } }