I’m using WordPress and in my theme’s function.php
I have a function to decode html entities and strip some tags:
function entities_and_strip($string, $allowable_tags) {
$result = html_entity_decode($string)
return strip_tags($result, $allowable_tags);
}
In single.php
I call this funciton:
echo entities_and_strip(get_the_content(), "<img><a>");
The problem is it strips a
tag as well and only allow img
tag. If I remove img
from allowable_tags
then it works.
I noticed two things so far:
- Passing
<img>
with any other tags (e.g.strip_tags($string, "<img><ul><li><a>");
) will result in stripping all tags except img. - This only happens when I’m in WordPress environment. I wrote a PHP script with
strip_tag
inside and run it separately. It worked well.
What makes the problem? And how could I solve it?
I’ve checked this function on my WordPress and it’s works, may be there is a problem with the content you are passing to the function , paste the content which you get from the function
get_the_content()
.