WordPress: Change Allowed Comments Html Tags [ allowed_tags() ]

Your blog readers can post comments with the html tags. You can display a list of all allowed HTML tags in the comments form using the following php code. Open comments.php (located in your themes directory) file and append the following code either before the comment form or submit button html code (please note that some themes have the following code, but it is usually commented out).

Allowed tags: <?php echo allowed_tags(); ?>

Change Allowed HTML Tags

Edit functions.php (located in your wp-content/themes/ directory) and append the following code:

/*
*  only allow <strong>, <em>, pre, code, and <a href=""> tags
*/
add_action('init', 'my_html_tags_code', 10);
function my_html_tags_code() {
  define('CUSTOM_TAGS', true);
  global $allowedposttags, $allowedtags;
  $allowedposttags = array(
      'strong' => array(),
      'em' => array(),
      'pre' => array(),
      'code' => array(),
      'a' => array(
        'href' => array (),
        'title' => array ())
  );
 
  $allowedtags = array(
      'strong' => array(),
      'em' => array(),
      'pre' => array(),
      'code' => array(),
      'a' => array(
        'href' => array (),
        'title' => array ())
  );
}

Save and close the file.

A Note About Thesis Theme

If you are using thesis theme add the following code in custom/custome_functions.php file:

function my_allowed_comment_tags() {
        $allowed_tags = '<span class="allowed"><span>' . "\n";
        $allowed_tags .= '<em>' . __('You can use these <acronym title="HyperText Markup Language">HTML</acronym> tags and attributes:', 'thesis') . '</em> ';
        $allowed_tags .= allowed_tags() . "\n";
        $allowed_tags .= '</span></span>' . "\n";
        echo apply_filters('thesis_allowed_tags', $allowed_tags);
}
add_action('thesis_hook_after_comment_box','my_allowed_comment_tags',9);

Save and close the file. Make sure you clear cache by visiting supercache or wp-cache setup page.

Meet the Author

Admin

I like chocolate, gadgets, open source software, photography, traveling and all shades of green colors. I love spending time with fun loving friends and family members. This is my own online journal.

10 comments… add one
  • Dan Jun 29, 2011, 6:10 am

    Geat stuff, just what I was looking for, thanks!

  • Dan Jun 29, 2011, 6:11 am

    Apparently I can’t type at 2 in the morning. *Great

  • Actuliens Jan 26, 2012, 4:56 pm

    Great, thks man

  • Shine Mar 13, 2012, 1:46 pm

    Thanks for this tutorial. I really appreciate it.

  • joe Apr 5, 2012, 12:18 am

    thanx is info, good sharing fren !

  • Raj Dec 11, 2013, 3:41 am

    Thanks to this post..

    I can now keep some of my hair !

  • angela Oct 29, 2012, 10:02 am

    Good tutorial for people

  • Rubel Dec 9, 2013, 8:26 pm

    Theme authors really do depend on their users to help build better themes.

  • Raj Dec 11, 2013, 3:42 am

    Good tips..

  • Irfan Apr 17, 2015, 6:51 pm

    thank you very much, thanks to the article above insights and information I became more widespread

Leave a Comment