Skip to content

Instantly share code, notes, and snippets.

@marcelo2605
Created August 2, 2018 16:09
Show Gist options
  • Save marcelo2605/804c29762947bb6b6ca04036409e05a7 to your computer and use it in GitHub Desktop.
Save marcelo2605/804c29762947bb6b6ca04036409e05a7 to your computer and use it in GitHub Desktop.
Replace <p> by <figure> around images and make tables responsive using table-responsive Bootstrap class
/**
* Replace <p> by <figure> around images and make tables responsive using table-responsive Bootstrap class
*/
add_filter( 'the_content', function($content) {
$content = preg_replace('/<p>\\s*?(<a .*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s', '<figure class="content-image">$1</figure>', $content);
// Run only if is a singular post page
if (is_singular()) {
$content = preg_replace("/(<table(?s).*<\/table>)/i", "<div class='table-responsive'>$1</div>", $content);
}
return $content;
}, 30 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment