I have searched high and low for a simple solution to this, but to no avail. WordPress keeps on wrapping my images in p tags and because of the eccentric nature of the layout for a site I am working on, this is highly annoying.
I have created a jQuery solution to unwrap images, but it isn’t that great. It lags because of other stuff loading on the page and so the changes are slow to be made. Is there a way to prevent WordPress wrapping just images with p tags? A hook or filter perhaps that can be run.
This is happening when uploading an image and then inserting it into the WYSIWYG editor. Manually going into the code view and removing the p tags is not an option as the client is not that technically inept.
I understand that images are inline, but the way I have the site coded images are inside of divs and set to block, so they are valid code.
here’s what we did yesterday on a client site that we were having this exact problem with… I created a quick filter as a plugin and activated it.
If you drop that into a php file in your /wp-content/plugins folder and then activate it, it should remove the p tags from any para that just contains an image.
I’m not sure how strong the regexp is in terms of if it will fail with outputs from other editors – for example if the img tag is closed with just > it will fail. If anyone has anything stronger, that would be really helpful.
Cheers,
James
— Improved filter —
To work with images that are wrapped in links, it keeps the links in the output and removes the p tags.
Basically you need to make WordPress treat img like block-level element for the purpose of formatting. Such elements are hardcoded in
wpautop()
and list is unfortunately not filtered.What I would do is:
wpautop()
under different name.img
to regexp in$allblocks
variable.wpautop
fromthe_content
filter.the_content
.maybe this will help
But then you are going to add the paragraphs for everything else manually.
This post is a little old, but there is a much simpler solution, barring CSS on your end.
Wrapping the img tag in a div has little negative effect.
Soska have given one/easy way.
But what I do is, extract image from content and display it separately.
I developed a plugin that fixed this exact issue:
http://wordpress.org/extend/plugins/unwrap-images/
It’s better than setting margin, or diving right into the WordPress code for those who don’t want to mess with code because it uses jQuery’s native unwrap function to unwrap all images of their p tags.
Hope this helps someone! Cheers, Brian
Accepted answer helped me with just the images but the revised code doesn’t handle linked images well at my site. This blog post has a code that works perfectly.
Here’s the code:
Cheers!
Iâm not an expert but just spent the hole afternoon trying to solve de img wraped in p tags and this worked for me.
I am working on a wordpress based theme and just added this to the functions.js file
Jquery function unwrap
now I can work p and img seperately.
Also can add a div with a different class arround the img using this:
this last one didnât solved my problem because I wanted to make p tags with display:none; so I really had to take those img out of there.
Depending on the post, another solution could be to use the WP Unformatted plugin to disable the auto-p function on a per post basis.
In case someone is look for a quick and dirty way to fix this for any tag here’s what I did:
$allblocks = '(?:table|thead|tfoot|capti...
img
,a
, etc…for example if it ends in
(...)menu|summary)';
change to(...)menu|summary|a)';
to add thea
tag and avoid autopeeing it. Note the pipe|
separator – it’s regex syntax!That’s it, happy WordPressing!
Put yout image inside a
<div>
tag, without any whitespace chars between them. So Instead of :You write this:
I’ve had the same problem with
<a>
elements, and this solved for me.