wppaste
WordPress

sanitize_post_field( string $field, mixed $value, int $post_id, string $context = 'display' ): mixed

Since
2.3.0, 4.4.0
Source
wp-includes/post.php:3025
Sanitizes a post field based on context.

Description

Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display' when calling filters. The 'sample' value is used for permalink previewing.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$fieldstring
The Post Object field name.
$valuemixed
The Post Object value.
$post_idint
Post ID.
$contextstringoptional
How to sanitize the field. Possible values are 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The 'sample' value is used for permalink previewing. Default 'display'.Default: 'display'

Return value

mixed
Sanitized value.

Performance profile

How much work a call to sanitize_post_field() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
11–53

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 138.

Plugin surface
9 hooks

Third-party callbacks on 'edit_{$field}', '{$field_no_prefix}_edit_pre', 'edit_post_{$field}' run inside this call, and their cost is not bounded by anything here.

Called by
5

5 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 11 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_post_field() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$field && $context === "raw"11–13none
$field15–17array_map()
!$field && $context !== "raw" && $context !== "edit" && $context !== "db" && $context !== "attribute" && $context !== "js"33–41apply_filters()
!$field && $context !== "raw"33–44apply_filters(), esc_attr()
!$field && $context !== "raw" && $context !== "edit" && $context === "db"34–41apply_filters(), apply_filters()
$context !== "raw" && $context === "edit" && $field !== "post_content"35–42apply_filters(), format_to_edit()
!$field && $context !== "raw" && $context !== "edit" && $context !== "db" && $context !== "attribute" && $context === "js"37–45apply_filters(), esc_js()
$context !== "raw" && $context === "edit" && $field === "post_content"38–45apply_filters(), user_can_richedit(), format_to_edit()
!$field && $context !== "raw" && $context === "edit"41–48apply_filters(), apply_filters(), esc_attr()
$context !== "raw" && $context === "edit" && $field !== "post_content"43–50apply_filters(), apply_filters(), format_to_edit()
$context !== "raw" && $context === "edit" && $field === "post_content"46–53apply_filters(), apply_filters(), user_can_richedit(), format_to_edit()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13811–5314
8.513811–5314
8.413811–53147 fewer instructions than PHP 8.3
8.314511–6014
8.214511–6014
8.114511–6014
7.414511–6014

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 9

9 hooks fire while sanitize_post_field() runs, in this order:

  1. apply_filters( edit_{$field} )filterline 3080 (+55 into the body)

    Filters the value of a specific post field to edit.

  2. apply_filters( {$field_no_prefix}_edit_pre )filterline 3111 (+86 into the body)

    Filters the value of a specific post field to edit.

  3. apply_filters( edit_post_{$field} )filterline 3135 (+110 into the body)

    Filters the value of a specific post field to edit.

  4. apply_filters( pre_{$field} )filterline 3178 (+153 into the body)

    Filters the value of a specific post field before saving.

  5. apply_filters( {$field_no_prefix}_save_pre )filterline 3208 (+183 into the body)

    Filters the value of a specific field before saving.

  6. apply_filters( pre_post_{$field} )filterline 3231 (+206 into the body)

    Filters the value of a specific field before saving.

  7. apply_filters( {$field}_pre )filterline 3254 (+229 into the body)

    Filters the value of a specific post field before saving.

  8. apply_filters( {$field} )filterline 3294 (+269 into the body)

    Filters the value of a specific post field for display.

  9. apply_filters( post_{$field} )filterline 3322 (+297 into the body)

    Filters the value of a specific post field for display.

Uses · 6

Used by · 5

Source code

function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {	$int_fields = array( 'ID', 'post_parent', 'menu_order' );	if ( in_array( $field, $int_fields, true ) ) {		$value = (int) $value;	} 	// Fields which contain arrays of integers.	$array_int_fields = array( 'ancestors' );	if ( in_array( $field, $array_int_fields, true ) ) {		$value = array_map( 'absint', (array) $value );		return $value;	} 	if ( 'raw' === $context ) {		return $value;	} 	$prefixed = false;	if ( str_contains( $field, 'post_' ) ) {		$prefixed        = true;		$field_no_prefix = str_replace( 'post_', '', $field );	} 	if ( 'edit' === $context ) {		$format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' ); 		if ( $prefixed ) { 			/**			 * Filters the value of a specific post field to edit.			 *			 * The dynamic portion of the hook name, `$field`, refers to the post			 * field name. Possible filter names include:			 *			 *  - `edit_post_author`			 *  - `edit_post_date`			 *  - `edit_post_date_gmt`			 *  - `edit_post_content`			 *  - `edit_post_title`			 *  - `edit_post_excerpt`			 *  - `edit_post_status`			 *  - `edit_post_password`			 *  - `edit_post_name`			 *  - `edit_post_modified`			 *  - `edit_post_modified_gmt`			 *  - `edit_post_content_filtered`			 *  - `edit_post_parent`			 *  - `edit_post_type`			 *  - `edit_post_mime_type`			 *			 * @since 2.3.0			 *			 * @param mixed $value   Value of the post field.			 * @param int   $post_id Post ID.			 */			$value = apply_filters( "edit_{$field}", $value, $post_id ); 			/**			 * Filters the value of a specific post field to edit.			 *			 * Only applied to post fields with a name which is prefixed with `post_`.			 *			 * The dynamic portion of the hook name, `$field_no_prefix`, refers to the			 * post field name minus the `post_` prefix. Possible filter names include:			 *			 *  - `author_edit_pre`			 *  - `date_edit_pre`			 *  - `date_gmt_edit_pre`			 *  - `content_edit_pre`			 *  - `title_edit_pre`			 *  - `excerpt_edit_pre`			 *  - `status_edit_pre`			 *  - `password_edit_pre`			 *  - `name_edit_pre`			 *  - `modified_edit_pre`			 *  - `modified_gmt_edit_pre`			 *  - `content_filtered_edit_pre`			 *  - `parent_edit_pre`			 *  - `type_edit_pre`			 *  - `mime_type_edit_pre`

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

4.4.0
Like sanitize_post(), $context defaults to 'display'.from the docblock
2.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.