sanitize_post

Sanitize every post field.

Description

(object|WP_Post|array) sanitize_post( (object|WP_Post|array) $post, (string) $context = 'display' ); 

If the context is raw,, then the post object or array will get minimal sanitization of the integer fields.

Returns (object|WP_Post|array)

The now sanitized Post Object or Array (will be the same type as $post).

Parameters (2)

0. $post (object|WP_Post|array)
The Post Object or Array
1. $context — Optional. (string) => 'display'
How to sanitize post fields. Accepts raw,, edit., db, or display. Default display.

Usage

  1. if ( !function_exists( 'sanitize_post' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // The Post Object or Array 
  5. $post = null; 
  6. $context = 'display'; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = sanitize_post($post, $context); 
  10.  

Defined (1)

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

/wp-includes/post.php  
  1. function sanitize_post( $post, $context = 'display' ) { 
  2. if ( is_object($post) ) { 
  3. // Check if post already filtered for this context. 
  4. if ( isset($post->filter) && $context == $post->filter ) 
  5. return $post; 
  6. if ( !isset($post->ID) ) 
  7. $post->ID = 0; 
  8. foreach ( array_keys(get_object_vars($post)) as $field ) 
  9. $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context); 
  10. $post->filter = $context; 
  11. } elseif ( is_array( $post ) ) { 
  12. // Check if post already filtered for this context. 
  13. if ( isset($post['filter']) && $context == $post['filter'] ) 
  14. return $post; 
  15. if ( !isset($post['ID']) ) 
  16. $post['ID'] = 0; 
  17. foreach ( array_keys($post) as $field ) 
  18. $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context); 
  19. $post['filter'] = $context; 
  20. return $post;