Created
March 7, 2018 10:37
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 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