Hello I try to keep only specific tag from an html string.

For example:

$content = '<a href="#">link</a> <b>strong text</b>';
$content = prefix_remove_specific_tag($content, $allowed_tag);
echo $content

Will return

<a href="#">link</a> strong text

I'm sure a wp core function existe for that, but i can't find it :(


I had to ask to finally find the solution, lol.

The wp_kses do exactly that:

$allowed_tags = array(
    'a'      => array(
        'href'      => array(),
$content = '<a href="#">link</a> <b>strong text</b>';
$content = wp_kses($content, $allowed_tags);

I found the solution in another topic

  • Just keep in mind that this is the opposite of how you phrased your original question. This code will remove everything except a specific tag. Not a specific tag. For example, if you only want to remove <b> tags, this won't work. That might be what you were always after, but I just wanted to point that out. – Jacob Peattie Oct 16 at 7:26
  • You're right, I edited the title to avoid confusion. – Robin ZecKa Ferrari Oct 16 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.