• Resolved nutesla

    (@nutesla)


    Hi there, πŸ™‚
    i’m new to WordPress and i like it very much πŸ˜€
    please, how can i replace with nothing…or <p>
    this reg expression
    <\s*div.*?>
    for every post…?
    simply i would to strip divs from post content…cause sometimes divs break my layout and i wanna prevent…
    thank you ^__^
    Regards

Viewing 9 replies - 1 through 9 (of 9 total)
  • You can use a filter to alter the_content. Code similar to this would go in your functions.php:

    <?php
    function filter_the_content($content) {
      $content = preg_replace('#<\s*div.*>#','',$content);
      return $content;
    }
    add_filter('the_content','filter_the_content');
    ?>

    However, I think you will find this a lot trickier than a simple replace because, for example, inserted images might be in a div. Also, the html may be encoded, so that instead of <div>, you might have & lt;div& gt;(without the spaces). Further, you need also to remove the ending tag:</div>.

    Thread Starter nutesla

    (@nutesla)

    sorry, if i try to insert that at the end of functions.php it breaks all my layout, i need it ONLY in the post content…not the whole blog πŸ˜€

    It ONLY affects the post content, but the content may have more in it than you expect.

    Thread Starter nutesla

    (@nutesla)

    and so, i don’t know why with the plugin “Universal post manager” i’m able to isolate post…but not with your function. I’m asking for this ’cause with that plugin, the embed youtube “fullscreen” option…doesn’t work anymore. I wrote to the support of that plugin…but nobody replied πŸ™

    It probably doesn’t work because the ‘Universal post manager’ puts its own divs in the content which the filter then removes, causing it to fail. As I said, there may be more in the content than you expect.

    You might be able to get around that by changing the priority of the filter. Try setting the priority to a lower number:

    add_filter('the_content','filter_the_content',0);

    Also, did you remember to remove the closing div tags?

    Thread Starter nutesla

    (@nutesla)

    excuse me, i wrote the code in the wrong place of functions.php, my mistake…it seems to work if only i’m able to remove the closing divs…but i don’t know how to do it…please help. i have too many posts to do it manually…thanks

    Try this:

    <?php
    function filter_the_content($content) {
      $content = preg_replace('#<\s*div.*>#','',$content);
      $content = preg_replace('#</div>#','',$content);
      return $content;
    }
    add_filter('the_content','filter_the_content');
    ?>
    Thread Starter nutesla

    (@nutesla)

    it works! you’re great! eheheh πŸ˜€ thank you

    You are welcome!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to execute a regex only for the POST content’ is closed to new replies.