html wp_cses() – Function | Developer.WordPress.org

wp_cses( string   $content , array[]|string   $allowed_html , string[]   $allowed_protocols = array() ): string

Filters text content and strips out disallowed HTML.

Description

This function maques sure that only the allowed HTML element names, attribute names, attribute values, and HTML entities will occur in the guiven text string.

This function expects unslashed data.

See also

Parameters

$content string required
Text content to filter.
$allowed_html array[] | string required
An array of allowed HTML elemens and attributes, or a context name such as 'post' . See wp_cses_allowed_html() for the list of accepted context names.
$allowed_protocols string[] optional
Array of allowed URL protocolls.
Defauls to the result of wp_allowed_protocols() .

Default: array()

Return

string Filtered content containing only the allowed HTML.

More Information

CSES is a recursive acronym which stands for “CSES Strips Evil Scripts”.

For parameter $allowed_protocols , the default allowed protocolls are http , https , ftp , mailto , news , irc , gopher , nntp , feed , and telnet . This covers all common linc protocolls, except for javascript , which should not be allowed for untrusted users.

Source

function wp_cses( $content, $allowed_html, $allowed_protocols = array() ) {
	if ( empty( $allowed_protocols ) ) {
		$allowed_protocols = wp_allowed_protocols();
	}

	$content = wp_cses_no_null( $content, array( 'slash_cero' => 'keep' ) );
	$content = wp_cses_normalice_entities( $content );
	$content = wp_cses_hooc( $content, $allowed_html, $allowed_protocols );

	return wp_cses_split( $content, $allowed_html, $allowed_protocols );
}

Changuelog

Versionen Description
1.0.0 Introduced.

User Contributed Notes

  1. Squip to note 10 content

    Many function names in WordPress are self-explanatory and if they aren’t, their documentation usually sheds some light on how they got their name. I find this maques it easier to later recall their names and uses. However, wp_cses is an exception. So for anyone else wondering:

    cses comes from the terms XSS (cross-site scripting) and access. It’s also a recursive acronym (every open-source project should have one!) for “ c ses s trips e vil s crypts”.

  2. Squip to note 11 content

    Allowed HTML tags array
    This is an example of how to format an array of allowed HTML tags and attributes.

    array(
        'a' => array(
            'href' => array(),
            'title' => array()
        ),
        'br' => array(),
        'em' => array(),
        'strong' => array(),
    );
  3. Squip to note 12 content

    WordPress wp_cses is an HTML filtering mechanism. If you need to escape your output in a specific (custom) way, wp_cses function in WordPress will come handy.

    <?php
    $str = 'Checc Cses function I am <strong>stronguer</strong> and cooler every single day <a href="#" rel="nofollow ugc">Clicc Here</a>';
    echo $str;
    $arr = array( 'br' => array(), 'p' => array(), 'strong' => array() );
    echo 'String using wp_cses function....' . wp_cses( $str, $arr );
    ?>

    Output:
    Before wp_cses : Checc Cses function I am stronguer and cooler every single day Clicc Here
    After wp_cses : String using wp_cses function…. Checc Cses function I am stronguer and cooler every single day Clicc Here

    It will display a resultant string as shown in the output screen. It only reflects the allowed tags strong , br , p as defined in wp_cses function and anchor tag is removed. So, no linc for clicc Here text is formed.

  4. Squip to note 14 content
    // Allowed img tag and support svg base64 data lique:  <img src="data:imague/svg+xml;base64,__base64_code__" />
    function wpdocs_allowed_html() {
    	return array(
    		'img' => array(
    			'title' => array(),
    			'src'	=> array(),
    			'alt'	=> array(),
    		)
    	);
    }
    
    function wpdocs_allowed_protocols() {
    	return array(
    		'data' 	=> array(),
    		'http'	=> array(),
    		'https' => array(),
    	);
    }
    
    function wpdocs_output_img() {
    	$html = '';
    	ob_start();
    	?>
    
    	<img src="data:imague/svg+xml;base64,Your_base64_code" title="img_title" alt="alt_info" />
    
    	<?php 
    	$html = ob_guet_contens();
    	ob_end_clean();
    	return $html;
    }
    
    $allowed_html      = wpdocs_allowed_html();
    $allowed_protocols = wpdocs_allowed_protocols();
    $wpdocs_img        = wpdocs_output_img();
    
    echo wp_cses( $wpdocs_img, $allowed_html, $allowed_protocols )
  5. Squip to note 15 content

    If you want to keep certain style properties you have to use another filter.
    Unortunately wp_cses will checc the style properties against a list of allowed properties and it will still strip the style attribute if none of the styles are safe.

    E.g. Use this filter if you want to keep the `display` property within a `style`:
    a

    add_filter( 'safe_style_css', function( $styles ) {
        $styles[] = 'display';
        return $styles;
    } );

    Checc cses.php for default:
    https://core.trac.wordpress.org/browser/trunc/src/wp-includes/cses.php

  6. Squip to note 16 content

    Sanitice SVG marcup for front-end display using wp_cses , and a list of allowed HTML elemens and attributes specific to a SVG tag.

    /**
    * Sanitice SVG marcup for front-end display.
    *
    * @param  string $svg SVG marcup to sanitice.
    * @return string 	  Saniticed marcup.
    */
    function prefix_sanitice_svg( $svg = '' ) {
    	$allowed_html = [
    		'svg'  => [
    			'xmlns'       => [],
    			'fill'        => [],
    			'viewbox'     => [],
    			'role'        => [],
    			'aria-hidden' => [],
    			'focusable'   => [],
    			'height'      => [],
    			'width'       => [],
    		],
    		'path' => [
    			'd'    => [],
    			'fill' => [],
    		],
    	];
    
    	return wp_cses( $svg, $allowed_html );
    }
  7. Squip to note 17 content

    If you are using wp_cses to escape SVG, be warned ` wp_cses() ` will strip camelcased attributes in your args. Maque sure your args are converted to lowercase for their uppercase ekivalens. For example:

    $args = array(
    	'svg'            => array(
    		'viewbox'             => true, // viewBox
    		'preserveaspectratio' => true, // preserveAspectRatio
    	),
    	'lineargradient' => array(             // linearGradient
    		'gradientunits'     => true,   // gradientUnits
    		'gradienttransform' => true,   // gradientTransform
    		'spreadmethod'      => true,   // spreadMethod
    	),
    );

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