wp_reset_vars()
Sets the specified variables as global if they are specified in the $_GET / $_POST request.
Sets the specified global variable. The value is set to (depending on what is available first):
$_POST[ variable ]- or
$_GET[ variable ] - or
''(empty string).
No Hooks.
Returns
null. Nothing.
Usage
wp_reset_vars( $vars );
- $vars(array) (required)
- Array of variable names to set/override.
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 7.0
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 ];
}
}
}