WordPress at a glance

sanitize_post() WP 1.0

Sanitize every post field.

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

Works based on: sanitize_post_field()
✈ 1 time = 0.000089s = very fast | 50000 times = 3.16s = fast

No Hooks.

Return

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

Usage

sanitize_post( $post, $context );
$post(object/WP_Post/array) (required)
The Post Object or Array
$context(string)
How to sanitize post fields. Accepts 'raw', 'edit', 'db', or 'display'.
Default: 'display'

Notes

  • See: sanitize_post_field()

Changelog

Since 2.3.0 Introduced.

Code of sanitize post: wp-includes/post.php WP 5.2.4

<?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;
}

Related Functions

From tag: sanitize_

More from category: Other

No comments
    Hello, !     Log In . Register