0

I am trying to create a function that will filter the_content depending on image alignment. So far I have this...

function filter_ptags_on_images($content)
{
 $content = preg_replace('/<p>\\s*?(<a .*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s', '$1', $content);
    return $content;
}

This is removing the P tag from images but I want to modify it so that it only does this is aligncenter is set on the image.

Does anybody have an example?

1 Answer 1

0

it should work like this.

function filter_ptags_on_images($content)
{
    $content = preg_replace('/<p>\s*?((<a.*?>)?<img[^>]+class="[^\"]*aligncenter[^\"]*".*?>(<\/a>)?)?\s*<\/p>/', '$1', $content);
    return $content;
}

it filters if the img tag has the class aligncenter and any class.

1
  • any news on this?
    – s t
    May 20, 2017 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.