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

Sanitize every post field.


Description Description

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

See also See also


Top ↑

Parameters Parameters

$post

(object|WP_Post|array) (Required) The Post Object or Array

$context

(string) (Optional) How to sanitize post fields. Accepts 'raw', 'edit', 'db', or 'display'.

Default value: 'display'


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-includes/post.php

2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
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;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.