I want to remove certain parent- and child-divs in all my wordpress posts with php or some other script

105 views Asked by At

Is there a quick way, via script maybe, to remove a certain pair of div's out of all my wordpress posts? For example:

I want to go from this:

<div class="single_textimage">
<div class="youtube_play"><iframe src="-,-"></iframe></div>
<div class="single_textimage_text">Some text.</div>
<div class="single_textimage_copyright">Some text.</div>
</div>

To this:

<div class="youtube_play"><iframe src="-,-"></iframe></div>

AND

From this:

 <div class="single_textimage">
<img class="aligncenter size-full wp-image-1700" src="-,-" />
<div class="single_textimage_text">Some text.</div>
<div class="single_textimage_copyright">Some text.</div>
</div>

To this:

<img class="aligncenter size-full wp-image-1700" src="-,-" />

So I want the divs: single_textimage, single_textimage_text and single_textimage_copyright to go.

I hope there is an easy script, or difficult for that matter. Via "php", "mysql" or "jquery" for example, that I can put in test.php in the root or something...

I hope I supplied you with enough information. If I haven't made myself clear enough, please reply. :)

2

There are 2 answers

0
AudioBubble On

Seems to me like you should be able to take those out of whatever template your using - probably in a PHP include, but I don't really use WordPress, so I wouldn't know where without seeing all your files. If you're bent on using jQuery instead of modifying the template, I would throw in some CSS too, to hide the elements that will be removed:

.single_textimage, .single_textimage_text, .single_textimage_copyright{
    display:none;
}

Then you can take the elements you want to keep out of their parent DIVs, and place them right after (or before):

$('.youtube_play, .wp-image-1700').each(function(){
   $(this).parent().after($(this)); 
});

Then you can remove the elements you don't want from the page:

$('.single_textimage, .single_textimage_text, .single_textimage_copyright').remove();

Here's a fiddle: https://jsfiddle.net/3uztorzL/

0
Mathew Tinsley On

I would use this search and replace utility to update all of the content in the DB:

https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

You'll need a regex to replace <div class="single_textimage_text">Some text.</div> (assuming the "some text" is different in each post). The utility supports regex replace. This may do it:

<div class="single_textimage_text">(.*?)</div>

Make sure you make a backup before you do the replace.