wp_kses() or how to leave in the text only allowed HTML tags and their attributes

This feature allows you to filter content and leave there only those HTML tags and their attributes that you want.

wp_kses ( $content, $html_tags, $protocols ) ;

$content
(string) (required) the content that you want to filter is placed here

$html_tags
(array) (required) an array listing the allowed HTML tags and their attributes

array(
    'a' => array(
        'href' => array(),
        'title' => array()
    ),
    'br' => array(),
    'em' => array(),
    'strong' => array(),
    'p' => array(),
    'h2' => array(),
    'img' => array(
        'alt' => array(),
        'src' => array()
    )
)

$protocols
(array) (optional) lists the allowed protocols. For example, the following protocols are allowed by default: http, https, ftp, mailto, news, irc, gopher, nntp, feed and telnet.

Returns the filtered text.

As an example, I’ll show the code that filters the content that is displayed using the get_the_content() function. In this example, it was not possible to use the the_content() function, so we used get_the_content() in conjunction with a filter that is embedded in the the_content() function.

$html_tags = array(
    'a' => array(
        'href' => array(),
        'title' => array()
    ),
    'br' => array(),
    'em' => array(),
    'strong' => array(),
    'p' => array(),
    'h2' => array(),
    'img' => array(
        'alt' => array(),
        'src' => array()
    )
);
 
echo wp_kses(apply_filters( 'the_content', get_the_content() ), $html_tags);