Using WordPress ‘allowed_tags()’ PHP function

The allowed_tags() WordPress PHP function is useful for displaying all the HTML tags and their respective attributes that are permitted in your WordPress site. This function becomes particularly handy in the comments section or for plugins that need to showcase the supported elements and attributes.

Usage

You can use the allowed_tags() function to get a string of all the allowed HTML tags. Here’s a simple example:

$allowed_html_tags = allowed_tags();
echo $allowed_html_tags;

This will output a list of all the allowed tags, something like:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Parameters

  • This function does not take any parameters.

More information

See WordPress Developer Resources: allowed_tags()
This function has been in WordPress since version 1.0.1 and there’s no deprecation notice so far.

Examples

Displaying Allowed Tags

If you want to inform your site users about the tags they can use, this function can be very handy.

// Get allowed tags
$allowed_html_tags = allowed_tags();

// Display them
echo "You can use the following HTML tags and attributes: " . $allowed_html_tags;

Store Allowed Tags

You may need to store the allowed tags in a variable for further use.

// Get and store allowed tags
$allowed_html_tags = allowed_tags();

Manipulating Allowed Tags

For instance, you might need to convert all of them to uppercase.

// Get allowed tags
$allowed_html_tags = allowed_tags();

// Convert them to uppercase
$allowed_html_tags_upper = strtoupper($allowed_html_tags);

// Display them
echo $allowed_html_tags_upper;

Counting Allowed Tags

You might want to count the number of allowed tags.

// Get allowed tags
$allowed_html_tags = allowed_tags();

// Count them
$allowed_html_tags_count = substr_count($allowed_html_tags, '<');

// Display the count
echo "The number of allowed tags is: " . $allowed_html_tags_count;

Filtering the Output

You could want to display only specific tags, such as ‘a’, ‘b’, and ‘i’.

// Get allowed tags
$allowed_html_tags = allowed_tags();

// Filter the tags
$filtered_tags = preg_match_all('/<a.*?>|<b.*?>|<i.*?>/', $allowed_html_tags, $matches);

// Display the filtered tags
foreach ($matches[0] as $tag) {
    echo $tag . " ";
}