So, I tried to strip all the html tags in a string but I can't get it to work.

This is my code snippet

$content = get_the_content();                                                                                                               
$content = preg_replace("/<embed[^>]+\>/i", "(embed) ", $content);                                                                                                                      
$content = apply_filters('the_content', $content);                                                                                                              
$content = str_replace(']]>', ']]>', $content);                                                                                                             
echo wp_strip_all_tags($content);

2 Answers 2

$content = get_the_content();
// regex (fixed) replacing '<embed>' with '(embed )'
$content = preg_replace("/<embed?[^>]+>/i", "(embed) ", $content);
// remove all tags
$content = wp_strip_all_tags($content);
echo $content;

Edited according to the first comment:

What you are trying to remove are not tags, these are HTML character entities. E. g., <p> was converted to &lt;p&gt; by WordPress when you've saved the content in the visual editor.

You have two choices:

  1. edit the content in the built-in text editor to remove those entities (switch the editor from Visual to HTML mode);
  2. use different regexes to remove entities after the post was fetched.

For example (not tested, but you have the idea):

$content = preg_replace("/&lt;embed?[.]+&gt/i", "(embed) ", $content);
$content = preg_replace("/&lt;[.]+&gt/i", "", $content);

If you want to strip all tags, you can use strip_tags. Small code example:

$content      = get_the_content();
$cleaned      = strip_tags($content, '<br>'); //keeps newlines
$superCleaned = strip_tags($content); //No HTML Tags
  • Thanks for your answer. This is the first function I worked with without a result. May 22, 2017 at 7:38
  • I am not sure @KonstantinosKoletsas. When using strip_tags, you are still having tags in your code?
    – xvilo
    May 22, 2017 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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