How to stop images from being wrapped in <p> tags?
I'm trying to stop the WordPress from automatically wrapping
<img>
s with
<p>
tags. I've tried this recommended snippet in my
functions.php
:
function filter_ptags_on_images($content){ // Remove p tags from around images
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');
This doesn't works if an image is the
first
element in a post or page. When an image comes before any text, the image gets wrapped with
<p>
tags once again. Any ideas?
Solutions
If you want to
remove
<p>
from entire content (not only from the images), this can help you
add_filter( 'the_content', 'wpse_226099_remove_p' );
function wpse_226099_remove_p( $content ) {
global $post;
return $post->post_content;
}
or maybe with this-
remove_filter('the_content', 'wpautop')
this is a function, that unwraps images from p tags inside the_content
/**
* WP: Unwrap images from <p> tag
* @param $content
* @return mixed
*/
function so226099_filter_p_tags_on_images( $content ) {
$content = preg_replace('/<p>\\s*?(<a .*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s', '\1', $content);
return $content;
}
add_filter('the_content', 'so226099_filter_p_tags_on_images');
Similar questions
Tinymce images auto-wrapped in <p> tag. CSS ways around or text editor hacks
Hiya, I have run into this problem many times now using drupal or wordpress where my tinymce config files are a bit too cleverly abstracted. The problem is that tinymce auto-wraps my <img> tags in <p> tags. If there is a way around this in either Wordpress or Drupal, that would be awesome. My problem exists when I want to do something l...
the_content() stop images being pulled through
I have created a custom post type to pull through the date, title, thumbnail and content, some of the posts have images and the_content() is pulling through those images and I don't know how to stop it from doing this, Could someone help me with this issue? My code:
How to stop Portrait Images being truncated using jQuery Cycle Lite Code
I am using the following Wordpress 3.1.1 with Nextgen Gallery and the JQuery Cycle Lite plugin. I have downloaded the latest version of the Cycle Lite Plug in from http://jquery.malsup.com/cycle/lite/ The problem I have is the Gallery I am using has some portrait images in and although I have fit set they are being chopped off rather than shrunk. S...
Wordpress - stop featured images from being cropped?
How do I stop the featured image of my posts from being cropped? This is the closest code I've found on functions.php: function thickbox_fields($form_fields, $post){ unset( $form_fields['image-size'] ); Thanks!!
Stop wordpress from adding html tags but not remove my own tags?
I've been trying to make wordpress stop adding html tags to my content but leave my own html tags alone? Tried remove_filter( 'the_content', 'wpautop' ); and installed PS Disable Auto Formatting but the problem is that it removes my own html tags aswell? I've tried to get_the_content() and the_content() but the same problem occurs there... The way ...
How to to stop html editor from addig <p> tags to shortcodes, images, etc
I know this this is covered as individual topics but What I am looking for is a single function that stops <p> tags from being wrapped around via the Wordpress editor to shortcodes and images and also removes empty <p> tages, for example: I have been using a few different functions that take $content and strips out the <p> tags vi...
Also ask