Does anyone know how I would automatically add <div class="table-responsive">
before every instance of a <table>
on a WordPress site using filter referencing? I would also need to add a </div>
to every instance of </table>
as well.
2 Answers
You can filter the_content
and use preg_replace()
to look for instances of <table></table>
and then surround them with your <div>
.
add_action( 'the_content', 'wpse_260756_the_content', 10, 1 );
function wpse_260756_the_content( $content ) {
$pattern = "/<table(.*?)>(.*?)<\/table>/i";
$replacement="<div class="table-responsive"><table$1>$2</table></div>";
return preg_replace( $pattern, $replacement, $content );
}