I have a filter that removes opening p tags and replaces closing p tags with a br tag sitewide. The client would like this filter in a shortcode that they can use only on specific portions of the site. Could the following code be adapted for use inside of a shortcode?

add_filter( 'the_content', 'remove_p' );
function remove_p( $content ) {
    $paragraphs = array("<p>","</p>");
    $noparagraphs = array("","<br>");
    return str_replace( $paragraphs, $noparagraphs, $content );

We've tried an assortment of plugins that toggle wpautop but were not happy with the results. Thanks in advance for any assistance.


You've got the $content available to you in your remove_p function - so inside that function just look for the existence of a special string (i.e. your "shortcode"), to allow the filter to do the str_replace. For example:

   if ( false !== strpos( $content, "[p-filter]") ) {
       $paragraphs = array("<p>","</p>","[p-filter]");
       $noparagraphs = array("","<br>","");
       return str_replace( $paragraphs, $noparagraphs, $content );
       return $content;

This will work as long as your client doesn't want to turn on/off your filter multiple times within a single POST object...if he does then it could still be done, but will be more complicated to implement.

  • Thank you C C for the assistance. Just what I was looking for. – B6431 Jun 23 '16 at 15:16

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.