Adding script code and other custom HTML in Category Descriptions

Forums Forums SwiftThemes Support Adding script code and other custom HTML in Category Descriptions

This topic contains 1 reply, has 1 voice, and was last updated by  James B 7 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #34602

    James B
    Participant

    I’m trying to add HTML code to category descriptions. WordPress allows basic HTML tags to be used but adding things like script tags etc. doesn’t work. WordPress automatically filters and removes them.

    I’ve done a bit of research and I now know a bit of code added to functions.php will remove these filters and allow basically any custom HTML to be used within category descriptions:

    foreach ( array( ‘pre_term_description’ ) as $filter ) {
    remove_filter( $filter, ‘wp_filter_kses’ );
    }
    foreach ( array( ‘term_description’ ) as $filter ) {
    remove_filter( $filter, ‘wp_kses_data’ );
    }

    The problem is the code to remove filters only works if added to the end of the PARENT theme functions.php file. If I add the code to my swift-child/functions.php the parent theme functions.php overrides. After a bit more research I learned this is because the swift-child/functions.php is loaded first and then the parent theme functions.php file is loaded later.

    Obviously I’d like to make these changes via a child theme so I don’t lose them with an update. Using the code to remove the filter within swift-child/functions.php doesn’t work because the filters are simply added back afterwards when the main function.php file is loaded. Obviously that’s not an issue with the theme and things are working as designed.

    I’m 90% to the solution. The problem is despite reading I can’t wrap my head around just how to properly format and add this code to swift-child/functions.php. I know I need to format it so it removes the filters AFTER the parent functions.php adds them. I just don’t understand how to format the code properly.

    I’m hoping for a little help but in the meantime I’ll continue looking into it.

    • This topic was modified 7 months, 1 week ago by  James B.
    #34604

    James B
    Participant

    I’m not having any luck with this. After doing some more reading it seems since the parent theme functions.php is loaded after the child theme’s I need to override the parent theme functions.php by either using “init” or “after_setup_theme” .

    I’ve tried adding the code listing in my first post as a function in swift-child/functions.php and naming it “remove_html_filters” for example.

    Attempting to remove filters with the ‘after_theme_setup’ hook doesn’t seem to work because there doesn’t appear to be any swift setup function in the main functions.php file. Maybe I’m missing something?

    I’ve also tried removing filters with add action and the “init” hook:
    add_action( ‘init’, ‘remove_html_filters, 11 )

    I’ve also tried assigning different priorities above 10 and even below 10 in an attempt to get this to work. No matter what I try I’m unable to add the code to remove HTML filters from category descriptions via my child theme functions.php file.

    If I add the code from my first post to the parent swift-premium/functions.php file it works fine. That’s what I’ll be doing as a workaround but I’d just love to figure out how to do this via my child theme so I don’t have to remember to make the changes again after core or theme updates. (I’m terrible at remembering this even with site changelog notes etc.)

    I’m admittedly not very good with PHP and digging into wordpress hooks, filters etc. so I’m sure there is a way to do this. I’m likely just overlooking something simple. I’m hoping someone can help!

    • This reply was modified 7 months ago by  James B.
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.