I’m having trouble with the wp_kses_allowed_html
filter in WordPress. It’s working fine in the admin area, but it doesn’t seem to do anything on the front-end of my site.
I’ve added this code to my plugin:
add_filter('wp_kses_allowed_html', 'my_custom_kses_filter', 10, 2);
function my_custom_kses_filter($allowed_tags, $context) {
// Log when the filter runs
error_log('KSES filter called');
// Add custom attribute to anchor tags
$allowed_tags['a']['data-my-custom'] = true;
return $allowed_tags;
}
I expected this to allow a custom data attribute on <a>
tags everywhere on my site. But it’s not working as I thought it would. Any ideas why this might be happening? Is there something I’m missing about how wp_kses_allowed_html
works on the front-end?
hmm, interesting problem! have u checked if wp_kses() is actually being called on ur front-end content? Sometimes themes or plugins bypass it. Maybe try adding a debug statement right before wp_kses() is called to see if it’s happening? also, wut context are u using for wp_kses? that can affect which tags are allowed too.
One aspect you might want to consider is the specific content you’re trying to filter on the front-end. The wp_kses_allowed_html filter is typically applied when WordPress sanitizes content, but this doesn’t happen automatically for all output. You may need to explicitly call wp_kses() or wp_kses_post() on your content where you want the filtering to occur. Additionally, ensure you’re not inadvertently overriding the filter elsewhere in your code. It might be worth adding some debugging code to verify the filter is being called as expected and that the modified $allowed_tags array contains your custom attributes when it’s used on the front-end.
hey liam, i’ve had similar issues before. try hooking into ‘wp_kses_allowed_html’ earlier, like in your theme’s functions.php or a mu-plugin. sometimes plugins load too late for this filter to work properly on the front-end. also, double-check if your content is actually being filtered by wp_kses() on the front-end. hope this helps!