wp_is_jsonp_request()
Checks if the current request is a JSONP request or expects a JSONP response.
Checks for the presence of the GET parameter _jsonp - $_GET['_jsonp']. The value of this parameter is passed through the function wp_check_jsonp_callback() and, if the check is passed, the function will return true.
1 time — 0.000001 sec (speed of light) | 50000 times — 0.01 sec (speed of light) | PHP 7.3.3, WP 5.2.3
Hooks from the function
Returns
true|false.
true— when the request expects a JSONP response ($_GET['_jsonp']contains the name of the callback and it is valid).false— in other cases.
Usage
wp_is_jsonp_request();
Examples
#1 Return JSONP data if it is a JSONP query
$data = [ 'foo'=>'bar' ];
if( wp_is_jsonp_request() ){
header( 'Content-Type: application/javascript; charset=utf-8' );
header( 'X-Content-Type-Options: nosniff' );
header( 'X-Robots-Tag: noindex' );
nocache_headers();
$jsonp_callback = $_GET['_jsonp'];
// Prepend '/**/' to mitigate possible JSONP Flash attacks.
// https://miki.it/blog/2014/7/8/abusing-jsonp-with-rosetta-flash/
echo '/**/' . $jsonp_callback . '(' . wp_json_encode( $data ) . ')';
}
Changelog
| Since 5.2.0 | Introduced. |
wp_is_jsonp_request() wp is jsonp request code WP 7.0
function wp_is_jsonp_request() {
if ( ! isset( $_GET['_jsonp'] ) ) {
return false;
}
if ( ! function_exists( 'wp_check_jsonp_callback' ) ) {
require_once ABSPATH . WPINC . '/functions.php';
}
$jsonp_callback = $_GET['_jsonp'];
if ( ! wp_check_jsonp_callback( $jsonp_callback ) ) {
return false;
}
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
$jsonp_enabled = apply_filters( 'rest_jsonp_enabled', true );
return $jsonp_enabled;
}