Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this code in my webpage...

<p style="text-align: center;">
   <a href="http://***.com/wp-content/uploads/2012/01/18169921.jpg">
      <img class="size-full wp-image-426 aligncenter" title="1816992" src="http://***.com/wp-content/uploads/2012/01/18169921.jpg" alt="" width="600" height="300">
   </a>
</p>

and I want it to be something like this. Replace p tag with div.

<div class="someclass">
   <a href="http://***.com/wp-content/uploads/2012/01/18169921.jpg">
      <img class="size-full wp-image-426 aligncenter" title="1816992" src="http://***.com/wp-content/uploads/2012/01/18169921.jpg" alt="" width="600" height="300">
   </a>
</div>

I have removed the <p> from the code using preg_replace() but not able to get how to add <div class="someclass"> in it while replacing the <p>.
Here is the code I have used. Plz help me to add div tag with some class.

function remove_ptags_on_images($content)
{
    return preg_replace('/<p .*>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'remove_ptags_on_images');

share|improve this question
Take a look at this question: stackoverflow.com/questions/5294209/preg-replace-p-with-br . You'll need to replace the <br /> with a <div> and adjust it for a closing </div>. – Jeremy Jared Feb 11 '12 at 8:17
2  
It appears you are looking for PHP scripting support using the function preg_replace(). This question would be better suited over at stackoverflow. Voted to close. – Brady Feb 11 '12 at 13:59
Thanks Brady, actually I am trying to wrap all wordpress images with div tag to apply custom class. Currently, all images are wrapped with p tag having style code in it. The code I used above is just removing p tags, I just need assitance how to add div in above preg_replace code. – Rick Feb 12 '12 at 4:51

closed as off topic by kaiser, Brady, toscho, Wyck, Chip Bennett Feb 12 '12 at 5:03

Questions on WordPress Answers are expected to relate to WordPress within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

Instead of writing a new function to replace text in your WordPress posts, you may also consider using a search plugin like Search Regex. It support regex as well.

share|improve this answer
Thanks Amit for your support, this plugin is good enough but I dont want to use external plugins. I am modifying my theme and want this functionality in it. – Rick Feb 12 '12 at 4:43

I have added the div tag in the second parameter of preg_replace() and its working very fine till now.

Here is the code...

return preg_replace('/<p .*>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '<div class="tutorial_image">\1\2\3</div>', $content);

Just want to confirm is it the correct way I have done or not.

Thanks

share|improve this answer

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