sanitize_post

The timeline below displays how wordpress function sanitize_post has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.7

/**
 * Sanitizes every post field.
 *
 * If the context is 'raw', then the post object or array will get minimal
 * sanitization of the integer fields.
 *
 * @since 2.3.0
 *
 * @see sanitize_post_field()
 *
 * @param object|WP_Post|array $post    The post object or array
 * @param string               $context Optional. How to sanitize post fields.
 *                                      Accepts 'raw', 'edit', 'db', 'display',
 *                                      'attribute', or 'js'. Default 'display'.
 * @return object|WP_Post|array The now sanitized post object or array (will be the
 *                              same type as `$post`).
 */
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;
}

WordPress Version: 5.6

/**
 * Sanitizes every post field.
 *
 * If the context is 'raw', then the post object or array will get minimal
 * sanitization of the integer fields.
 *
 * @since 2.3.0
 *
 * @see sanitize_post_field()
 *
 * @param object|WP_Post|array $post    The post object or array
 * @param string               $context Optional. How to sanitize post fields.
 *                                      Accepts 'raw', 'edit', 'db', or 'display'.
 *                                      Default 'display'.
 * @return object|WP_Post|array The now sanitized post object or array (will be the
 *                              same type as `$post`).
 */
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;
}

WordPress Version: 4.4

/**
 * Sanitize every post field.
 *
 * If the context is 'raw', then the post object or array will get minimal
 * sanitization of the integer fields.
 *
 * @since 2.3.0
 *
 * @see sanitize_post_field()
 *
 * @param object|WP_Post|array $post    The Post Object or Array
 * @param string               $context Optional. How to sanitize post fields.
 *                                      Accepts 'raw', 'edit', 'db', or 'display'.
 *                                      Default 'display'.
 * @return object|WP_Post|array The now sanitized Post Object or Array (will be the
 *                              same type as $post).
 */
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;
}

WordPress Version: 4.0

/**
 * Sanitize every post field.
 *
 * If the context is 'raw', then the post object or array will get minimal
 * sanitization of the integer fields.
 *
 * @since 2.3.0
 *
 * @see sanitize_post_field()
 *
 * @param object|WP_Post|array $post    The Post Object or Array
 * @param string               $context Optional. How to sanitize post fields.
 *                                      Accepts 'raw', 'edit', 'db', or 'display'.
 *                                      Default 'display'.
 * @return object|WP_Post|array The now sanitized Post Object or Array (will be the
 *                              same type as $post).
 */
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;
    } else {
        // 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;
}

WordPress Version: 3.7

/**
 * Sanitize every post field.
 *
 * If the context is 'raw', then the post object or array will get minimal santization of the int fields.
 *
 * @since 2.3.0
 * @uses sanitize_post_field() Used to sanitize the fields.
 *
 * @param object|WP_Post|array $post The Post Object or Array
 * @param string $context Optional, default is 'display'. How to sanitize post fields.
 * @return object|WP_Post|array The now sanitized Post Object or Array (will be the same type as $post)
 */
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;
    } else {
        // 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;
}