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:2965
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.
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'. 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
- Scaling
- Constant
- Instructions
- 11–53
- Plugin surface
- 9 hooks
- Called by
- 5
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 137.
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.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$field && $context === "raw" | 11–13 | none |
$field | 14–16 | array_map() |
!$field && $context !== "raw" && $context !== "edit" && $context !== "db" && $context !== "attribute" && $context !== "js" | 33–41 | apply_filters() |
!$field && $context !== "raw" | 33–44 | apply_filters(), esc_attr() |
!$field && $context !== "raw" && $context !== "edit" && $context === "db" | 34–41 | apply_filters(), apply_filters() |
$context !== "raw" && $context === "edit" && $field !== "post_content" | 35–42 | apply_filters(), format_to_edit() |
!$field && $context !== "raw" && $context !== "edit" && $context !== "db" && $context !== "attribute" && $context === "js" | 37–45 | apply_filters(), esc_js() |
$context !== "raw" && $context === "edit" && $field === "post_content" | 38–45 | apply_filters(), user_can_richedit(), format_to_edit() |
!$field && $context !== "raw" && $context === "edit" | 41–48 | apply_filters(), apply_filters(), esc_attr() |
$context !== "raw" && $context === "edit" && $field !== "post_content" | 43–50 | apply_filters(), apply_filters(), format_to_edit() |
$context !== "raw" && $context === "edit" && $field === "post_content" | 46–53 | apply_filters(), apply_filters(), user_can_richedit(), format_to_edit() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 137 | 11–53 | 14 | |
| 8.5 | 137 | 11–53 | 14 | |
| 8.4 | 137 | 11–53 | 14 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 144 | 11–60 | 14 | |
| 8.2 | 144 | 11–60 | 14 | |
| 8.1 | 144 | 11–60 | 14 | |
| 7.4 | 144 | 11–60 | 14 |
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:
- apply_filters( edit_{$field} )filterline 3020 (+55 into the body)
Filters the value of a specific post field to edit.
- apply_filters( {$field_no_prefix}_edit_pre )filterline 3051 (+86 into the body)
Filters the value of a specific post field to edit.
- apply_filters( edit_post_{$field} )filterline 3075 (+110 into the body)
Filters the value of a specific post field to edit.
- apply_filters( pre_{$field} )filterline 3118 (+153 into the body)
Filters the value of a specific post field before saving.
- apply_filters( {$field_no_prefix}_save_pre )filterline 3148 (+183 into the body)
Filters the value of a specific field before saving.
- apply_filters( pre_post_{$field} )filterline 3171 (+206 into the body)
Filters the value of a specific field before saving.
- apply_filters( {$field}_pre )filterline 3194 (+229 into the body)
Filters the value of a specific post field before saving.
- apply_filters( {$field} )filterline 3233 (+268 into the body)
Filters the value of a specific post field for display.
- apply_filters( post_{$field} )filterline 3260 (+295 into the body)
Filters the value of a specific post field for display.
Uses · 6
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- format_to_edit()Acts on text which is about to be edited.
- user_can_richedit()Determines whether the user can access the visual editor.
- esc_attr()Escaping for HTML attributes.
- esc_js()Escapes single quotes, `"`, ``, `&`, and fixes line endings.
Used by · 5
- WP_Post::__get()Getter.
- get_post_field()Retrieves data from a post field based on Post ID.
- post_exists()Determines if a post exists based on title, content, date and type.
- sanitize_post()Sanitizes every post field.
- set_post_type()Updates the post type for the post ID.
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', $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.
Signature, return type and hooks compared across 5 parsed releases.
sanitize_post(), $context defaults to 'display'.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.