rest_sanitize_request_arg

Sanitize a request argument based on details registered to the route.

Description

rest_sanitize_request_arg( (mixed) $value, (WP_REST_Request) $request, (string) $param ); 

Parameters (3)

0. $value (mixed)
The value.
1. $request (WP_REST_Request)
The request.
2. $param (string)
The param.

Usage

  1. if ( !function_exists( 'rest_sanitize_request_arg' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/vendor/wp-rest-functions.php'; 
  3.  
  4. // The value. 
  5. $value = null; 
  6.  
  7. // The request. 
  8. $request = null; 
  9.  
  10. // The param. 
  11. $param = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = rest_sanitize_request_arg($value, $request, $param); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/vendor/wp-rest-functions.php  
  1. function rest_sanitize_request_arg( $value, $request, $param ) { 
  2.  
  3. $attributes = $request->get_attributes(); 
  4. if ( ! isset( $attributes['args'][ $param ] ) || ! is_array( $attributes['args'][ $param ] ) ) { 
  5. return $value; 
  6. $args = $attributes['args'][ $param ]; 
  7.  
  8. if ( 'integer' === $args['type'] ) { 
  9. return (int) $value; 
  10.  
  11. if ( 'boolean' === $args['type'] ) { 
  12. return rest_sanitize_boolean( $value ); 
  13.  
  14. if ( isset( $args['format'] ) ) { 
  15. switch ( $args['format'] ) { 
  16. case 'date-time' : 
  17. return sanitize_text_field( $value ); 
  18.  
  19. case 'email' : 
  20. /** 
  21. * sanitize_email() validates, which would be unexpected 
  22. */ 
  23. return sanitize_text_field( $value ); 
  24.  
  25. case 'uri' : 
  26. return esc_url_raw( $value ); 
  27.  
  28. case 'ipv4' : 
  29. return sanitize_text_field( $value ); 
  30.  
  31. return $value;