Sanitizes every post field.
Description
If the context is ‘raw’, then the post object or array will get minimal sanitization of the integer fields.
See also
Parameters
$post
object|WP_Post|arrayrequired- The post object or array
$context
stringoptional- How to sanitize post fields.
Accepts'raw'
,'edit'
,'db'
,'display'
,'attribute'
, or'js'
. Default'display'
.Default:
'display'
Source
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;
}
Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.