Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boywondercreative/7a5501bf4498a4cf59ae971970e2b86b to your computer and use it in GitHub Desktop.
Save boywondercreative/7a5501bf4498a4cf59ae971970e2b86b to your computer and use it in GitHub Desktop.
Prevent Wordpress from wrapping images and iframes in p tags (default WYSIWYG and Advanced Custom Fields Wordpress WYSIWYG Plugin)
/**
*
* Prevent Wordpress from wrapping images and iframes in p tags
* http://css-tricks.com/snippets/wordpress/remove-paragraph-tags-from-around-images/
* ( <p> and <iframe> and ACF support - http://wordpress.stackexchange.com/questions/136840/how-to-remove-p-tags-around-img-and-iframe-tags-in-the-acf-wysiwyg-field
*/
// Default Wordpress WYSIWYG
function filter_ptags_on_images_iframes($content)
{
$content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('the_content', 'filter_ptags_on_images_iframes');
// ACF WYSIWYG Plugin
function filter_ptags_on_images_iframes_acf($content)
{
$content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('acf_the_content', 'filter_ptags_on_images_iframes_acf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment