Forums

Way to strip HTML from the_content ? (6 posts)

  1. jimrb22
    Member
    Posted 3 years ago #

    Problem:

    Go here: http://blog.filmrot.com/
    Note one of the Twitter posts
    Hover the "ReTweet" link - notice that the text inside of the link contains HTML

    I need a way to strip all html from a post. My Google-fu is failing me and I've searched the codex and the plugin directory. I even tried to do it with pure PHP. I know there has to be a way to do this, I'm just not finding it. Hoping someone here knows of a method.

    Here is the pure PHP method I tried within TheLoop, with zero result...

    $content2strip = the_content('Read the rest of this entry »');
    echo strip_tags($content2strip);

    Thanks! --James

  2. hunger
    Member
    Posted 2 years ago #

    <?php
    		ob_start();
    		the_content();
    		$old_content = ob_get_clean();
    		$new_content = strip_tags($old_content);
    		echo $new_content;
    ?>
  3. Otto
    Tech Ninja
    Posted 2 years ago #

    Use get_the_content() instead of the_content(), to get the content into a variable.

  4. hunger
    Member
    Posted 2 years ago #

    Otto42, thanks for the tip re get_the_content(); I wasn't aware of that function!

  5. muaythai
    Member
    Posted 2 years ago #

    I used:

    <?php
    ob_start();
     the_content(''.__('Read more <span class="meta-nav">&raquo;</span>', 'sandbox').'');
    $old_content = ob_get_clean();
    $new_content = strip_tags($old_content);
    echo $new_content;
    ?>

    without get_the content() and it worked just fine except all the HTML was stripped out of the post- even linebreaks and a drop-cap created in my stylesheet. Basically turned all posts into giant blocks of text with no style, even from my CSS.

    Is there any way to strip out HTML formatting only from text that's been input via the Dashboard editor? I basically want users to not be allowed to use HTML in their posts.

  6. adamluz
    Member
    Posted 2 years ago #

    To allow some code like <p> and <br /> you add those tags after like this:

    $new_content = strip_tags($old_content, '<p><a><b><br /><input><form><img><textarea><li><ol><ul><table>');

    the above example will allow:
    <p><a><b><br /><input><form><img><textarea><li><ol><ul><table>

Topic Closed

This topic has been closed to new replies.

About this Topic