Reputation: 797
i am trying to remove p tag from wordpress that automatically adds p tag in images.Also i center the images with wordpress so it becomes something like this
so i tried adding this code
function filter_ptags_on_images($content){
return preg_replace('/<p style=".*?">\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
add_filter('the_content', 'filter_ptags_on_images');
This removes style from p tag and it become something like this
<p><img src=...><p>
Can anyone help me how to remove p from images only.
Upvotes: 2
Views: 2418
Reputation: 3614
Try below code:
function filter_ptags_on_images($content)
{
return preg_replace('/<p>(\s*)(<img .* \/>)(\s*)<\/p>/iU', '\2', $content);
}
add_filter('the_content', 'filter_ptags_on_images');
Or try to modify your own code:
function filter_ptags_on_images($content)
{
$content = preg_replace('/<p style=".*?">\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
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');
Upvotes: 2
Reputation: 715
using this hack you had remove P tag from your content please use this hack and let me know once this work fine.
remove_filter ('the_content', 'wpautop');
remove_filter ('comment_text', 'wpautop');
Upvotes: 1