astra_addon_sanitize_css_value( string $value, string $type = 'spacing' )

Sanitize a CSS value based on the expected type.


Description

Use this for any stored value written into a generated stylesheet: esc_attr() and esc_url() are not CSS-context escapes and leave the characters that close a rule.


Parameters

$value

(Required) The raw value to sanitize.

$type

(Optional) The type of CSS value. Accepts 'spacing' (appends px to plain numbers), and since 4.13.11 'color', 'length', 'number' (a plain number for callers that append the unit) and 'url'. Default 'spacing'.

Default value: 'spacing'


Return

(string) Sanitized value, or empty string if invalid.


Source

File: classes/astra-common-functions.php

function astra_addon_sanitize_css_value( $value, $type = 'spacing' ) {
	$value = sanitize_text_field( $value );
	if ( '' === $value ) {
		return '';
	}

	switch ( $type ) {
		case 'spacing':
			if ( is_numeric( $value ) ) {
				return $value . 'px';
			}
			return preg_match( '/^\d*\.?\d+(px|em|rem|%|vh|vw)$/', $value ) ? $value : '';

		default:
			return '';
	}
}

Changelog

Changelog
Version Description
4.12.5 Introduced.


User Contributed Notes

You must log in before being able to contribute a note or feedback.