Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What is the best way to strip *the_content* of html tags and needless spaces on the front end? I'm building a special use theme that needs to allow users who can edit_post to make changes on the front end, but I only want this text area to support plain text.

My current code:

<?php if ( !current_user_can( 'edit_post', $post_ID ) ) : the_content('Read more on "'.the_title('', '', false).'" &raquo;'); else: ?>
        <textarea class="grid_22">
                <?php the_content('Read more on "'.the_title('', '', false).'" &raquo;'); ?>
        </textarea>
<?php endif ?>

produces this: the_content wrapped in a textarea tag

How can I make it look like normal text? Users who edit copy on the front end will not use the backend WYSIWYG editor so I don't have to worry about formats being over written.

share|improve this question
    
You could try get_the_content() or $post->post_content (i think this will give you the raw content), but additionally just call strip_tags ... eg. strip_tags( .. whatever you want to strip tags from .. ); –  t31os Feb 17 '11 at 16:19
add comment

2 Answers

up vote 0 down vote accepted

You will have to worry about formats beeing over written.

Ex.

<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. ____Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p> <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua._____ At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>

Ends up with having the part between the "_" over written. Even if you edit it as plain text, you will loose the closing and opening paragraph, so two paragraphs become only one.

Everything else you asked for can be done with filters on the_content or get_the_content (please take a look at the codex for further explanation). You'll need to use str_replace or preg_replace (native php functions, please see php.net for explanation) inside those filters.

share|improve this answer
    
Can you provide a working example of how to strip html out? I'm a designer who is trying to expand into programing and I'm still trying to learn php. :) –  mattrepublic Feb 17 '11 at 17:02
    
You could use the native php strip_tags function: php.net/manual/de/function.strip-tags.php –  kaiser Feb 17 '11 at 17:05
    
Thanks for the reference kaiser! –  mattrepublic Feb 17 '11 at 23:07
    
@M.Taylor: If it is the solution, please mark it (check below arrows for upvoting). It helps you "accept rate" - you currently got only 71%. –  kaiser Feb 18 '11 at 1:14
add comment

The HTML tag "textarea" .... takes into account spaces.....

e.g.: http://stackoverflow.com/questions/2202999/why-is-textarea-filled-with-mysterious-white-spaces

share|improve this answer
    
That solves the spaces issue. Thanks! –  mattrepublic Feb 17 '11 at 1:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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