Function and usage of allowed_tags() in wordpress

Question

Answers ( 1 )

    0
    2024-01-05T13:08:45+00:00

    The allowed_tags() function in WordPress is a handy tool for developers and site administrators, particularly when it comes to managing content that involves HTML elements. Here's a detailed explanation and sample usage of this function:

    Function Signature:

    allowed_tags(): string
    

    Functionality:

    • Purpose: The primary function of allowed_tags() is to display all the allowed HTML tags, along with their attributes.
    • Usage Context: This function is particularly useful in scenarios where you want to inform users about the HTML tags and attributes that are supported in a certain area of your site, like the comment section.

    Description:

    • Displaying Allowed Tags: When you use allowed_tags(), it returns a string containing HTML-formatted list of all allowed HTML tags and their respective attributes.
    • Use Case in Comments Area: It's common to display this information in the comment area of a WordPress site. This helps users understand which HTML tags they can use while writing comments.
    • Integration with Plugins: For plugins that need to display or work with allowed HTML tags, allowed_tags() provides an easy way to retrieve this information.

    Return:

    • The function returns a string. This string is an HTML representation of allowed tags, with each tag and its attributes being entity encoded.

    Sample Usage:

    1. Displaying in the Comment Form: You might want to inform users about the allowed HTML tags in the comment form. To do this, you can add the following code in your comments.php template or wherever the comment form is displayed:

      echo '<div class="allowed-tags">';
      echo '<p>You can use the following HTML tags:</p>';
      echo '<code>' . allowed_tags() . '</code>';
      echo '</div>';
      
    2. Use in a Plugin: If you're developing a plugin and need to display or process allowed HTML tags, you can use allowed_tags() to get the list and then manipulate or display it as required.

      $allowed_tags = allowed_tags();
      // Further processing or display of $allowed_tags
      

    Notes:

    • Customizing Allowed Tags: WordPress allows customization of allowed HTML tags and attributes through various filters like wp_kses_allowed_html. This means the output of allowed_tags() can change if these filters are used.
    • Security Considerations: It’s important to understand that allowing certain HTML tags can have security implications, especially in areas where users can input data.

    By using allowed_tags(), WordPress developers and administrators can effectively manage and display the HTML capabilities of different parts of their site, ensuring both flexibility and security in user interactions.

Leave an answer