Webxopt

Turn a Website into a Business

T 07 3103 3259
M +61 409 825 759
E info@webxopt.com

  • Home
  • Why Us?
  • What We Do
  • B2B Websites
    • Case Study
  • REST API
  • Cybersecurity
  • Consulting
  • Articles
  • Contact
    • Privacy
    • Help Page

Tablepress CSS Column Selector

June 1, 2018

Tablepress CSS Column Selector Plugin adds the number of columns in a Tablepress table as a class on the table. This then allows the table to be styled with standard CSS based on the number of columns.

The origin of this plugin was when I had a page with many tables, and the columns just didn’t line up how I wanted. My options were to manually add the column widths in tablepress or to style them with CSS.

Result

Initial Code
<table id=”tablepress-2-no-107″ class=”tablepress tablepress-id-2″>

After Plugin (for a 3 column table)
<table id=”tablepress-2-no-107″ class=”tablepress tablepress-id-2 column-3“>

Typical CSS

You will need to manually add the CSS as per your requirement, but can now target the number of table columns. In the example below I have the last column always set to 20% (in this particular case it was so a button in the table displayed in a single column).

table.tablepress-id-2.column-2 td.column-1{width:80%;}
table.tablepress-id-2.column-2 td.column-2{width:20%;}
table.tablepress-id-2.column-3 td.column-1{width:54%;}
table.tablepress-id-2.column-3 td.column-2{width:26%;}
table.tablepress-id-2.column-3 td.column-3{width:20%;}
table.tablepress-id-2.column-4 td.column-1{width:54%;}
table.tablepress-id-2.column-4 td.column-2{width:13%;}
table.tablepress-id-2.column-4 td.column-3{width:13%;}
table.tablepress-id-2.column-4 td.column-4{width:20%;}

To download this plugin (it got stuck in WordPress review and I haven’t been able to get in touch with the person that keeps emailing me). You can download directly from https://www.dropbox.com/s/kpzfgggqjscf2k6/tablepress-css-col-selector.zip?dl=1

Please note that this is not the greatest thing to do securitywise! The code though is below if you want to copy and paste it into your functions.php file instead.

// JavaScript Document
jQuery(document).ready(function(){
		var numbertablesonpage = jQuery("table.tablepress").length;

var tableid = [];
jQuery("table.tablepress").each(function () {
   tableid.push(jQuery(this).attr("id"));
});
		
	jQuery.each( tableid, function( index, value ){
    	var tableidnow = tableid[index];
		var colclass1 = jQuery("table#" + tableidnow + " tr.row-2 td:last-child").attr('class');
		jQuery("table#" + tableidnow ).addClass(colclass1);
	});
	
});

This is a javascript document containing some jquery that should be saved separately in the active theme file /js/ tablecolresizer.js

You can see that all it does is looks at a table that is loading, then it takes the last cell of the second row, and copies the class, which is the column number. It them applies that same class to the top level table, giving you an way to style the table based on the number of columns it contains.

To add this file into your theme, add the following into your themes functions.php file.

// The following adds the script that allows a tablepress table to check the number of columns int he table
function tp-col-indicator() {
    wp_enqueue_script( 'tp-col-indicator', 'https://YOUR-SITE/wp-content/themes/YOUR-THEME/js/tablecolresizer.js', array( 'jquery' ), 1.0, true );
}
add_action( 'wp_enqueue_scripts', 'tp-col-indicator' );

Obvious replace YOUR-SITE and YOUR-THEME in the code above.

Filed Under: Articles

Recent Posts

  • AliCloud (Aliyun) Server Crashes Fixed with the Help of Site24x7
  • S/MIME Certificate – Sectigo Usage
  • Cybersecurity for Australian Small/Micro Business – Networks 2 – DNS
  • Cybersecurity for Australian Small/Micro Business – Networks 1 – Routers
  • Cybersecurity for Australian Small/Micro Business – Mobile, Part 2

Advanced Help

Enter your code in here. You will need to contact me first for a code!

Simon Griffiths in San Francisco

Webxopt was formed in 2009 by Simon Griffiths, who was at the time working as the Marketing Manager of a medium sized company in the electrical industry. Simons background includes study of both … Read More

Awards – Graduate Certificate in Cybersecurity

Robina Town Centre - Webxopt

Webxopt Pty Ltd
PO Box 16
Robina Town Centre
QLD 4230, Australia

T 07 3103 3259
E info@webxopt.com
ABN: 24 624 264 951

© 2025 · Webxopt · Built on the Genesis Framework