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
if ( !function_exists( 'sanitize_post' ) ) { require_once ABSPATH . WPINC . '/post.php'; } // The Post Object or Array $post = null; $context = 'display'; // NOTICE! Understand what this does before running. $result = sanitize_post($post, $context);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/post.php
- function sanitize_post( $post, $context = 'display' ) {
- if ( is_object($post) ) {
- // Check if post already filtered for this context.
- if ( isset($post->filter) && $context == $post->filter )
- return $post;
- if ( !isset($post->ID) )
- $post->ID = 0;
- foreach ( array_keys(get_object_vars($post)) as $field )
- $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
- $post->filter = $context;
- } elseif ( is_array( $post ) ) {
- // Check if post already filtered for this context.
- if ( isset($post['filter']) && $context == $post['filter'] )
- return $post;
- if ( !isset($post['ID']) )
- $post['ID'] = 0;
- foreach ( array_keys($post) as $field )
- $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
- $post['filter'] = $context;
- }
- return $post;
- }