sanitize_term_field( string $field, string $value, int $term_id, string $taxonomy, string $context ): mixed
- Since
- 2.3.0
- Source
wp-includes/taxonomy.php:1786
Description
Passing a term field value through the function should be assumed to have cleansed the value for whatever context the term field is going to be used.
If no context or an unsupported context is given, then default filters will be applied.
There are enough filters for each context to support a custom filtering without creating your own filter function. Simply create a function that hooks into the filter you need.
Compatibility
- WordPress
- since 2.3.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- Term field to sanitize.
$valuestring- Search for this term value.
$term_idint- Term ID.
$taxonomystring- Taxonomy name.
$contextstring- Context in which to sanitize the term field.
Accepts 'raw', 'edit', 'db', 'display', 'rss', 'attribute', or 'js'.
Return value
mixed- Sanitized field.
Performance profile
How much work a call to sanitize_term_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
- 14–59
- Plugin surface
- 9 hooks
- Called by
- 8
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 135.
Third-party callbacks on 'edit_term_{$field}', 'edit_{$taxonomy}_{$field}', 'pre_term_{$field}' run inside this call, and their cost is not bounded by anything here.
8 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 · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost sanitize_term_field() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$context === "raw" | 14–19 | strtolower() |
$context !== "raw" && $context !== "edit" && $context !== "attribute" && $context !== "js" | 42–52 | strtolower(), apply_filters(), apply_filters() |
$context !== "raw" | 45–55 | strtolower(), apply_filters(), apply_filters(), esc_attr() |
$context !== "raw" && $context !== "edit" && $context !== "attribute" && $context === "js" | 46–56 | strtolower(), apply_filters(), apply_filters(), esc_js() |
$context !== "raw" && $context === "edit" && $field === "description" && $context !== "attribute" && $context !== "js" | 47–54 | strtolower(), apply_filters(), apply_filters(), esc_html() |
$context !== "raw" && $context !== "edit" && $context === "db" && $field === "slug" && $context !== "attribute" && $context !== "js" | 48–55 | strtolower(), apply_filters(), apply_filters(), apply_filters() |
$context !== "raw" && $context === "edit" && $field !== "description" && $context === "attribute" | 50–57 | strtolower(), apply_filters(), apply_filters(), esc_attr(), esc_attr() |
$context !== "raw" && $context === "edit" && $field === "description" && $context === "attribute" | 50–57 | strtolower(), apply_filters(), apply_filters(), esc_html(), esc_attr() |
$context !== "raw" && $context !== "edit" && $context === "db" && $field === "slug" && $context === "attribute" | 51–58 | strtolower(), apply_filters(), apply_filters(), apply_filters(), esc_attr() |
$context !== "raw" && $context === "edit" && $field !== "description" && $context !== "attribute" && $context === "js" | 51–58 | strtolower(), apply_filters(), apply_filters(), esc_attr(), esc_js() |
$context !== "raw" && $context === "edit" && $field === "description" && $context !== "attribute" && $context === "js" | 51–58 | strtolower(), apply_filters(), apply_filters(), esc_html(), esc_js() |
$context !== "raw" && $context !== "edit" && $context === "db" && $field === "slug" && $context !== "attribute" && $context === "js" | 52–59 | strtolower(), apply_filters(), apply_filters(), apply_filters(), esc_js() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 135 instructions, 14–59 executed per call, 11 branches. The work does not change between versions.
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_term_field() runs, in this order:
- apply_filters( edit_term_{$field} )filterline 1814 (+28 into the body)
Filters a term field to edit before it is sanitized.
- apply_filters( edit_{$taxonomy}_{$field} )filterline 1827 (+41 into the body)
Filters the taxonomy field to edit before it is sanitized.
- apply_filters( pre_term_{$field} )filterline 1846 (+60 into the body)
Filters a term field value before it is sanitized.
- apply_filters( pre_{$taxonomy}_{$field} )filterline 1858 (+72 into the body)
Filters a taxonomy field before it is sanitized.
- apply_filters( pre_category_nicename )filterline 1871 (+85 into the body)
Filters the category nicename before it is sanitized.
- apply_filters( term_{$field}_rss )filterline 1885 (+99 into the body)
Filters the term field for use in RSS.
- apply_filters( {$taxonomy}_{$field}_rss )filterline 1897 (+111 into the body)
Filters the taxonomy field for use in RSS.
- apply_filters( term_{$field} )filterline 1913 (+127 into the body)
Filters the term field sanitized for display.
- apply_filters( {$taxonomy}_{$field} )filterline 1927 (+141 into the body)
Filters the taxonomy field sanitized for display.
Uses · 4
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_html()Escaping for HTML blocks.
- esc_attr()Escaping for HTML attributes.
- esc_js()Escapes single quotes, `"`, ``, `&`, and fixes line endings.
Used by · 8
- WP_Media_List_Table::column_default()Handles output for the default column.
- WP_Posts_List_Table::column_default()Handles the default column output.
- WP_Query::parse_tax_query()Parses various taxonomy related query vars.
- WP_Term_Query::get_terms()Retrieves the query results.
- get_term_field()Gets sanitized term field.
- get_the_category_rss()Retrieves all of the post categories, formatted for use in feeds.
- sanitize_category_field()Sanitizes data in single category key field.
- sanitize_term()Sanitizes all term fields.
Source code
function sanitize_term_field( $field, $value, $term_id, $taxonomy, $context ) { $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); if ( in_array( $field, $int_fields, true ) ) { $value = (int) $value; if ( $value < 0 ) { $value = 0; } } $context = strtolower( $context ); if ( 'raw' === $context ) { return $value; } if ( 'edit' === $context ) { /** * Filters a term field to edit before it is sanitized. * * The dynamic portion of the hook name, `$field`, refers to the term field. * * @since 2.3.0 * * @param mixed $value Value of the term field. * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. */ $value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy ); /** * Filters the taxonomy field to edit before it is sanitized. * * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer * to the taxonomy slug and taxonomy field, respectively. * * @since 2.3.0 * * @param mixed $value Value of the taxonomy field to edit. * @param int $term_id Term ID. */ $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id ); if ( 'description' === $field ) { $value = esc_html( $value ); // textarea_escaped } else { $value = esc_attr( $value ); } } elseif ( 'db' === $context ) { /** * Filters a term field value before it is sanitized. * * The dynamic portion of the hook name, `$field`, refers to the term field. * * @since 2.3.0 * * @param mixed $value Value of the term field. * @param string $taxonomy Taxonomy slug. */ $value = apply_filters( "pre_term_{$field}", $value, $taxonomy ); /** * Filters a taxonomy field before it is sanitized. * * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer * to the taxonomy slug and field name, respectively. * * @since 2.3.0 * * @param mixed $value Value of the taxonomy field. */ $value = apply_filters( "pre_{$taxonomy}_{$field}", $value ); // Back compat filters. if ( 'slug' === $field ) { /** * Filters the category nicename before it is sanitized. * * Use the {@see 'pre_$taxonomy_$field'} hook instead.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.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/taxonomy.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.