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

by admin on December 15, 2010 · 9 comments

in Wordpress

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).

  1.  
  2. Allowed tags: <?php echo allowed_tags(); ?>
  3.  

Change Allowed HTML Tags

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

  1.  
  2. /*
  3. * only allow <strong>, <em>, pre, code, and <a href=""> tags
  4. */
  5. add_action('init', 'my_html_tags_code', 10);
  6. function my_html_tags_code() {
  7. define('CUSTOM_TAGS', true);
  8. global $allowedposttags, $allowedtags;
  9. $allowedposttags = array(
  10. 'strong' => array(),
  11. 'em' => array(),
  12. 'pre' => array(),
  13. 'code' => array(),
  14. 'a' => array(
  15. 'href' => array (),
  16. 'title' => array ())
  17. );
  18.  
  19. $allowedtags = array(
  20. 'strong' => array(),
  21. 'em' => array(),
  22. 'pre' => array(),
  23. 'code' => array(),
  24. 'a' => array(
  25. 'href' => array (),
  26. 'title' => array ())
  27. );
  28. }
  29.  

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:

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

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

{ 9 comments… read them below or add one }

1 Dan June 29, 2011 at 6:10 am

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

Reply

2 Dan June 29, 2011 at 6:11 am

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

Reply

3 Actuliens January 26, 2012 at 4:56 pm

Great, thks man

Reply

4 Shine March 13, 2012 at 1:46 pm

Thanks for this tutorial. I really appreciate it.

Reply

5 joe April 5, 2012 at 12:18 am

thanx is info, good sharing fren !

Reply

6 Raj December 11, 2013 at 3:41 am

Thanks to this post..

I can now keep some of my hair !

Reply

7 angela October 29, 2012 at 10:02 am

Good tutorial for people

Reply

8 Rubel December 9, 2013 at 8:26 pm

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

Reply

9 Raj December 11, 2013 at 3:42 am

Good tips..

Reply

Previous post: