wppaste
WordPress

safecss_filter_attr( string $css, string $deprecated = '' ): string

Since
2.8.1, 4.4.0, 4.6.0, 5.0.0, 5.1.0, 5.2.0, 5.3.0, 5.3.1, 5.7.1, 5.8.0, 6.1.0, 6.2.0, 6.3.0, 6.4.0, 6.5.0, 6.6.0, 6.9.0
Source
wp-includes/kses.php:2565
Filters an inline style attribute and removes disallowed rules.

Compatibility

WordPress
since 6.9.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

$cssstring
A string of CSS rules, decoded from an HTML style attribute.
$deprecatedstringoptional
Not used.Default: ''

Return value

string
Filtered string of CSS rules, needing HTML escaping before sending back to a style attribute.

Performance profile

How much work a call to safecss_filter_attr() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
28–36

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

Plugin surface
2 hooks

Third-party callbacks on 'safe_style_css', 'safecss_filter_attr_allow_css' run inside this call, and their cost is not bounded by anything here.

Called by
9

9 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 query, option, cache, serialize 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 · 2 distinct outcomes

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

WhenInstructionsCalls it makes
empty($deprecated)28–32wp_kses_no_null(), wp_allowed_protocols(), explode(), apply_filters()
!empty($deprecated)32–36_deprecated_argument(), wp_kses_no_null(), wp_allowed_protocols(), explode(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14828–3624
8.514828–3624
8.414828–362448 fewer instructions than PHP 8.3
8.319633–4124
8.219633–4124
8.119633–41242 fewer instructions than PHP 7.4
7.419833–4124

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 · 2

2 hooks fire while safecss_filter_attr() runs, in this order:

  1. apply_filters( safe_style_css )filterline 2585 (+20 into the body)

    Filters the list of allowed CSS attributes.

  2. apply_filters( safecss_filter_attr_allow_css )filterline 2882 (+317 into the body)

    Filters the check for unsafe CSS in `safecss_filter_attr`.

Uses · 7

Used by · 9

Source code

function safecss_filter_attr( $css, $deprecated = '' ) {	if ( ! empty( $deprecated ) ) {		_deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented.	} 	$css = wp_kses_no_null( $css );	$css = str_replace( array( "\n", "\r", "\t" ), '', $css ); 	$allowed_protocols = wp_allowed_protocols(); 	/** @todo Parse enough CSS to split rules without breaking on things like quoted strings. */	$css_array = explode( ';', trim( $css ) ); 	/**	 * Filters the list of allowed CSS attributes.	 *	 * @since 2.8.1	 *	 * @param string[] $attr Array of allowed CSS attributes.	 */	$allowed_attr = apply_filters(		'safe_style_css',		array(			'background',			'background-color',			'background-image',			'background-position',			'background-repeat',			'background-size',			'background-attachment',			'background-blend-mode', 			'border',			'border-radius',			'border-width',			'border-color',			'border-style',			'border-right',			'border-right-color',			'border-right-style',			'border-right-width',			'border-bottom',			'border-bottom-color',			'border-bottom-left-radius',			'border-bottom-right-radius',			'border-bottom-style',			'border-bottom-width',			'border-bottom-right-radius',			'border-bottom-left-radius',			'border-left',			'border-left-color',			'border-left-style',			'border-left-width',			'border-top',			'border-top-color',			'border-top-left-radius',			'border-top-right-radius',			'border-top-style',			'border-top-width',			'border-top-left-radius',			'border-top-right-radius', 			'border-spacing',			'border-collapse',			'caption-side', 			'columns',			'column-count',			'column-fill',			'column-gap',			'column-rule',			'column-span',			'column-width', 			'display', 			'color',			'filter',			'font',			'font-family',

Changelog

Introduced in 2.8.1. 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.

6.9.0
Added support for white-space.from the docblock
6.6.0
Added support for grid-column, grid-row, and container-type.from the docblock
6.5.0
Added support for background-repeat.from the docblock
6.4.0
Added support for writing-mode.from the docblock
6.3.0
Extended support for filter to accept a URL and added support for repeat().
Added support for box-shadow.from the docblock
6.2.0
Added support for aspect-ratio, position, top, right, bottom, left, and z-index CSS properties.from the docblock
6.1.0
Added support for min(), max(), minmax(), clamp(), nested var() values, and assigning values to CSS variables.
Added support for object-fit, gap, column-gap, row-gap, and flex-wrap.
Extended margin-* and padding-* support for logical properties.from the docblock
5.8.0
Added support for calc() and var() values.from the docblock
5.7.1
Added support for object-position.from the docblock
5.3.1
Added support for gradient backgrounds.from the docblock
5.3.0
Added support for grid, flex and column layout properties.
Extended background-* support for individual properties.from the docblock
5.2.0
Added support for background-position and grid-template-columns.from the docblock
5.1.0
Added support for text-transform.from the docblock
5.0.0
Added support for background-image.from the docblock
4.6.0
Added support for list-style-type.from the docblock
4.4.0
Added support for min-height, max-height, min-width, and max-width.from the docblock
2.8.1
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/kses.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.