1

I have to implement a default class for HTML-Tables at WordPress. The wp-standard is <table>

.. ist there any way to do this, that i get this output per default? <table class="abc xyz">

maybe with filters?

2
  • 1
    What table are you taking about? Sep 15 '13 at 20:45
  • normal html table ... <table></table>. I want to add a class autmatically so i dont have to do this at every post.
    – hazelnut
    Sep 15 '13 at 20:47
3

In your case you need to add custom hook for the_content filter:

add_filter( 'the_content', 'wpse8170_add_custom_table_class' );
function wpse8170_add_custom_table_class( $content ) {
    return str_replace( '<table>', '<table class="mycustom-class">', $content );
}

Add this snippet to your functions.php file.

0
2

I made a simple modification to get the code recognize other styles within the tag:

add_filter( 'the_content', 'wpse8170_add_custom_table_class' );
function wpse8170_add_custom_table_class( $content ) {
return str_replace( '<table', '<table class="my-class"', $content );
}
0

There is no table functionality in the default editors, visual or text, so unless are using an editor like CKEditor that does generate that markup, I'd use a shortcode.

function table_wrapper_wpse_114253( $atts, $content = null ) {
  return '<table class="abc xyz">'.$content.'</table>';
}
add_shortcode('table','table_wrapper_wpse_114253');

In your content, just write [table]...[/table] instead of <table>...</table>

I have to add that if your theme uses body_class and post_class correctly, and all you want to do is add a class to every <table> in the post body, there is almost certainly existing markup in place that makes what you are doing unnecessary, whether you use a filter on the_content as @EugeneManuilov suggests or a shortcode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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