How to Remove the P Tag from Around Images in the_content in WordPress

Home » Snippets » How to Remove the P Tag from Around Images in the_content in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever noticed that WordPress often wraps your images in paragraph tags in the_content field? While it’s not necessarily a major issue, it might interfere with your website’s layout, especially for those who are meticulous about their design. If you’re among those people, you’re probably wondering if there’s a way to remove the paragraph tags. The good news is – yes, there absolutely is. In this article, we will demonstrate how to effectively remove the "

" tags from around images in the_content, streamlining your site’s aesthetics and functionality.

					function wpturbo_remove_ptag_from_images($content){
    return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/s', '123', $content);
}
add_filter('the_content', 'wpturbo_remove_ptag_from_images');
				

The given code snippet is used to remove the paragraph tags <p> that WordPress automatically wraps around images in your content. This code comes handy if you wish for a cleaner HTML structure in your content or if you want to customize the layout around your images.

The function wpturbo_remove_ptag_from_images($content) is declared initially, this function will do most of the heavy lifting in the code. It’s defined to accept one parameter, $content, which represents the post’s content here.

function wpturbo_remove_ptag_from_images($content){
    return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/s', '123', $content);
}

Inside the function, preg_replace function is used. preg_replace is a PHP function that performs a pattern match for a string, and it replaces it with a specified string.

The pattern to be matched is given by the regular expression / <p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/s. This regex pattern is looking for an <img> tag that is wrapped in a <p> tag, the <img> tag could also be a child of <a> tag (a link tag).

The regular expression could be broken down into several sections to understand how it works:

  • <p>\s*: Look for the paragraph opening tag <p> which may be followed by any number of white spaces.
  • (<a .*>)?\s*: This section is looking for an optional opening <a> tag.
  • <img .* \/>\s* : Looks for an <img> tag.
  • (<\/a>)?\s*: Looking for an optional closing </a> tag.
  • <\/p>: Finally, it looks for the closing </p> tag.

If found, it will replace the whole match (the image wrapped with <p> tag) with what was inside the <p> tag using '123'. Here, 1, 2, and 3 are backreferences that refer to the content matched by the 3 sets of parentheses in the regular expression. In a nutshell, the preg_replace() function is used to take out the <p> tag wrapped around the images in our content.

The final step is adding the newly created function to the WordPress filter the_content using add_filter() function.

add_filter('the_content', 'wpturbo_remove_ptag_from_images');

The ‘the_content’ filter allows plugins to process the post content after WordPress gets it from the database, but before it is displayed to users. So, when WordPress prepares your post content for display, it will first pass the content to our wpturbo_remove_ptag_from_images($content) function.

In this way, we can manipulate the content to remove <p> tag from around images. To sum up, this code simply takes the post’s content as input, searches for images wrapped in paragraph tags, removes those tags, and returns the cleaned-up content.

Register an account to save your snippets or go Pro to get more features.