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
- Source
wp-includes/kses.php:2373
Compatibility
- WordPress
- since 6.6.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.
$deprecatedstringoptional- Not used.Default:
''
Return value
string- Filtered string of CSS rules.
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
- Scaling
- Scales with input
- Instructions
- 28–36
- Plugin surface
- 2 hooks
- Called by
- 8
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 148.
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.
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 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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($deprecated) | 28–32 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 148 | 28–36 | 24 | |
| 8.5 | 148 | 28–36 | 24 | |
| 8.4 | 148 | 28–36 | 24 | 48 fewer instructions than PHP 8.3 |
| 8.3 | 196 | 33–41 | 24 | |
| 8.2 | 196 | 33–41 | 24 | |
| 8.1 | 196 | 33–41 | 24 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 198 | 33–41 | 24 |
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:
- apply_filters( safe_style_css )filterline 2392 (+19 into the body)
Filters the list of allowed CSS attributes.
- apply_filters( safecss_filter_attr_allow_css )filterline 2686 (+313 into the body)
Filters the check for unsafe CSS in `safecss_filter_attr`.
Uses · 7
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- wp_kses_no_null()Removes any invalid control characters in a text string.
- wp_allowed_protocols()Retrieves a list of protocols to allow in HTML attributes.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_kses_bad_protocol()Sanitizes a string and removed disallowed URL protocols.
Used by · 8
- WP_Navigation_Block_Renderer::get_responsive_container_markup()Get the responsive container markup
- WP_Style_Engine_CSS_Declarations::filter_declaration()Filters a CSS property + value pair.
- WP_Theme_JSON::is_safe_css_declaration()Checks that a declaration provided by the user is safe.
- get_block_core_post_featured_image_overlay_element_markup()Generate markup for the HTML element that will be used for the overlay.
- render_block_core_post_featured_image()Renders the `core/post-featured-image` block on the server.
- styles_for_block_core_search()Builds an array of inline styles for the search block.
- wp_get_layout_style()Generates the CSS corresponding to the provided layout.
- wp_kses_attr_check()Determines whether an attribute is allowed.
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(); $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', 'color', 'filter', 'font', 'font-family', 'font-size', 'font-style', 'font-variant',Changelog
Introduced in 2.8.1. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
grid-column, grid-row, and container-type.from the docblockbackground-repeat.from the docblockwriting-mode.from the docblockfilter to accept a URL and added support for repeat().Added support for
box-shadow.from the docblockaspect-ratio, position, top, right, bottom, left, and z-index CSS properties.from the docblockmin(), 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 docblockcalc() and var() values.from the docblockobject-position.from the docblockgrid, flex and column layout properties.Extended
background-* support for individual properties.from the docblockbackground-position and grid-template-columns.from the docblocktext-transform.from the docblockbackground-image.from the docblocklist-style-type.from the docblockmin-height, max-height, min-width, and max-width.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.