adding custom classes for tables

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">

Read More

maybe with filters?

Related posts

3 comments

  1. 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.

  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 );
    }
    
  3. 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.

Comments are closed.