wordpress – functions.php – preg_replace – <div<-tags

i have a problem with the posts of wordpress. Using the code embed function of an external blog publishing automatically on wordpress, the html code messes up my styling.

I do have the following code:

Read More
    <div class="entry-content">
     <div style="width:450px;margin:0 auto">
      <div style="position:relative;">
       <a target="_blank" href="DIFFERENT VARIABLES">
        <img width="???" alt="DIFFERENT VARIABLES" src="DIFFERENT VARIABLES" title="DIFFERENT VARIABLES" height="???" />
       </a>
      </div>
     </div>
     <p><br/>
     <div style="text-align:center"><small>SEVERAL <a>-TAGS</small></div>
    </div>

The goal is to get rid of the second <div> with the style attributes and to get rid of the <p><br/>.

The best way would be a function for the function.php cause I do have lo of posts.

Can anyone help me please? I’m good enough to do some html coding, but this is far beyond my skills.

Related posts

Leave a Reply

2 comments

  1. function my_function($content) {
        $div = '<div style="width:450px;margin:0 auto">';
        // Make sure the offending div is there.
        if (strpos($content, $div) !== false) {
             // Remove div.
             $content = str_replace($div, '', $content);
             // Remove one ending div tag.
             $content = str_replace('</div>', '', $content, 1);
             // Remove the p, br.
             $content = str_replace('<p><br/>', '', $content, 1);
        }
        return $content
    }
    add_filter('the_content', 'my_function');
    

    Add this to your functions.php.

  2. Have you tried a jQuery command to remove it, such as jQuery('.entry-content br').remove() ?

    At the php level, you could use the_content filter to change the contents.