How to remove p tag from wordpress

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

Read More
function filter_ptags_on_images($content){
return preg_replace('/<p style=".*?">s*(<a .*>)?s*(<img .* />)s*(</a>)?s*</p>/iU',           '123', $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.

Related posts

Leave a Reply

2 comments

  1. 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',           '123', $content);
        return preg_replace('/<p>s*(<a .*>)?s*(<img .* />)s*(</a>)?s*</p>/iU', '123', $content);
    }   
    add_filter('the_content', 'filter_ptags_on_images');
    
  2. 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');