WordPress.org

Ready to get started?Download WordPress

Forums

Category description strips html tags (16 posts)

  1. vavroom
    Member
    Posted 6 years ago #

    I want to be able to use my own HTML in the category description, allowing me to add lists, or break paragraphs where I want, while retaining valid (x)HTML.

    Eventually, I'll use the description to display it on each category page.

    Unfortunately, WP seems to over-write all tags I manually put in there. Most annoying.

    Any ideas on how to solve? I've sifted through codex and forum and google, I can't seem to find a solution to this.

    Thanks

  2. Adam Brown
    Member
    Posted 6 years ago #

    What sort of HTML are we talking about? I use p, strong, and em tags in my category descriptions regularly without problems.

  3. vavroom
    Member
    Posted 6 years ago #

    p were stripped out.

    ol, ul and li were stripped out.

    a href was left intact.

    blockquote was stripped out.

  4. runen
    Member
    Posted 6 years ago #

    I also would love to be able to include html and even php code in category description.

  5. Arno Esterhuizen
    Member
    Posted 6 years ago #

    In wp-includes/default-filters.php, comment out add_filter($filter, 'wp_filter_kses');, like shown below (line 29).

    $filters = array('pre_term_description', 'pre_link_description', 'pre_link_notes', 'pre_user_description');
    foreach ( $filters as $filter ) {
          //add_filter($filter, 'wp_filter_kses');
    }

    A better option might be to write a plugin to do it, but I can't help you there -- yet.

    Cheers
    Arno

  6. Arno Esterhuizen
    Member
    Posted 6 years ago #

    OK, so that was easy...

    Copy the text below into a text file and save it as html-in-category-descriptions.php in your plugins directory.

    Cheers
    Arno Esterhuizen

    <?php
    /*
      Plugin Name: HTML in Category Descriptions
      Version: 1.0
      Plugin URI:
      Description: Allows you to add HTML code in category descriptions
      Author: Arno Esterhuizen
      Author URI: ****
    */
    
    $filters = array('pre_term_description', 'pre_link_description', 'pre_link_notes', 'pre_user_description');
    foreach ( $filters as $filter ) {
        remove_filter($filter, 'wp_filter_kses');
    }
    
    ?>
  7. mores
    Member
    Posted 6 years ago #

    Thank you *so* much, arno. I actually just pasted your code (including copyright comments ;) ) into my existing "functions.php" inside my template directory, and it works like that too.
    Just what I needed!

  8. rooseve
    Member
    Posted 6 years ago #

    I've got the same problem, I want to find some plugin that will keep the post content in category page with part of html, not part of raw text, expecially I want to keep some pics in the post.

  9. sophiavancouver
    Member
    Posted 6 years ago #

    I figured out how to hack the default-filters.php file (referred to above) for version 2.6, which has changed what they call category_description. It's now called 'term_description'. I did a hack and slash and commented out anything filtering the term description including the below.

    // Kses only for textarea saves
    $filters = array(/*'pre_term_description',*/ 'pre_link_description', 'pre_link_notes', 'pre_user_description');
    foreach ( $filters as $filter ) {
    	add_filter($filter, 'wp_filter_kses');
    }

    and this bit:
    'code'

    // Format text area for display.
    //$filters = array('term_description');
    //foreach ( $filters as $filter ) {
    //	add_filter($filter, 'wptexturize');
    //	add_filter($filter, 'convert_chars');
    //	add_filter($filter, 'wpautop');
    //}

    Worked for me.
    Sophia Kelly

  10. mores
    Member
    Posted 5 years ago #

    The problem with this is that when you update WordPress, your hack will be overwritten with the newest default-filters.php file.

    Using a plugin or a functions.php solution is update-proof. Most of the time :)

  11. t0b1lle
    Member
    Posted 5 years ago #

    This broke with 2.6.1 for me, i had to add
    pre_term_description, just add the following block to functions.php in your theme.

    $filters = array('term_description','category_description','pre_term_description');
    foreach ( $filters as $filter ) {
    remove_filter($filter, 'wptexturize');
    remove_filter($filter, 'convert_chars');
    remove_filter($filter, 'wpautop');
    remove_filter($filter, 'wp_filter_kses');
    remove_filter($filter, 'strip_tags');
    }

  12. bonanza9
    Member
    Posted 5 years ago #

    html comments are still filtered out. any idea how to stop this? - i'd like to make my own <!--more--> function for descriptions...

  13. houseofstrauss
    Member
    Posted 5 years ago #

    arno_esterhuizen solution is preferable. As already said, hacking the core is not a good idea, if you can do it with a plugin. I tried this plugin with 2.7 and it works perfectly. Thanks arno_esterhuizen!

  14. rEd86
    Member
    Posted 5 years ago #

    OK, I have installed this plug-in and it works as expected, but there's a problem. The Category Description is also used as the TITLE for the category navigation link, so there's now HTML in the link!

    Is there a way to extend the plugin so it overrides what is return to the "wp_list_categories" call and strips out the HTML in that situation specifically?

    Thanks to arno_esterhuizen for a nice plug-in solution.

    --Ed

  15. shalako
    Member
    Posted 5 years ago #

    Using arno_esterhuizen's method in 2.7 and it works great.

    2 issues:

    1. echo rEd86's point: mousover the category in navigation and the whole description, including html markup, pops up.

    2. I want to conditionally display the description if it exists. I've tried this:
    if (category_description()) { echo category_description(); }
    but it doesn't work. If category description is empty, it's still echos a line break.

  16. mores
    Member
    Posted 5 years ago #

    use wp_list_categories(use_desc_for_title=0)

    Shalako: try adding a trim() like this: if (category_description()) { echo trim(category_description()); }

Topic Closed

This topic has been closed to new replies.

About this Topic