TablePress Shortcodes

Below, you’ll find a detailed guide on how to add the TablePress Shortcodes to your WordPress website, including their parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the TablePress – Tables in WordPress made easy Plugin shortcodes not to show or not to work correctly.

Before starting, here is an overview of the TablePress Plugin and the shortcodes it provides:

Plugin Icon
TablePress – Tables in WordPress made easy

"TablePress is a user-friendly plugin that simplifies creating and managing tables in WordPress. Ideal for organizing data, creating comparison charts, or showcasing product features."

★★★★★ (4496) Active Installs: 800000+ Tested with: 6.3.2 PHP Version: 5.6.20
Included Shortcodes:
  • [tablepress]
  • [table-info]

TablePress [tablepress] Shortcode

The TablePress shortcode is used to display and manage data in tables within WordPress. This shortcode supports a variety of attributes allowing customization of table content, layout, and functionality. It checks if a table with a given ID exists, loads the table data, and handles error scenarios. The shortcode also enables caching for table output and integrates with DataTables JavaScript library for advanced features.

Shortcode: [tablepress]

Parameters

Here is a list of all possible tablepress shortcode parameters and attributes:

  • id – Unique identifier of the table
  • alternating_row_colors – Toggle alternate row coloring
  • datatables_sort – Enable or disable table sorting
  • datatables_paginate – Turn on or off table pagination
  • datatables_paginate_entries – Set number of entries per page
  • datatables_lengthchange – Allow or disallow change in number of entries per page
  • datatables_filter – Enable or disable table filtering
  • datatables_info – Show or hide table information
  • datatables_scrollx – Enable or disable horizontal scrolling
  • datatables_scrolly – Enable or disable vertical scrolling
  • datatables_locale – Set the language for the table
  • datatables_custom_commands – Add custom commands for the table
  • cache_table_output – Enable or disable caching of table output
  • shortcode_debug – Enable or disable debugging of shortcode

Examples and Usage

Basic Example – A simple way to display a table with a specific ID.

[table id=1 /]

Advanced examples

Display a table with a specific ID and enable DataTables features like sorting, pagination, and filtering.

[table id=1 datatables_filter=true datatables_sort=true datatables_paginate=true /]

Display a table with a specific ID and disable DataTables features like sorting, pagination, and filtering.

[table id=1 datatables_filter=false datatables_sort=false datatables_paginate=false /]

Display a table with a specific ID, enable DataTables features and specify the number of entries for pagination.

[table id=1 datatables_paginate_entries=10 /]

Display a table with a specific ID and enable horizontal scrolling for wide tables.

[table id=1 datatables_scrollx=true /]

Display a table with a specific ID and enable vertical scrolling for long tables.

[table id=1 datatables_scrolly=true /]

Display a table with a specific ID and specify a custom command for DataTables.

[table id=1 datatables_custom_commands="order: [[ 1, 'asc' ]]" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [tablepress] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( TablePress::$shortcode, array( $this, 'shortcode_table' ) );

Shortcode PHP function:

function shortcode_table( $shortcode_atts ) {
		// Don't use `array` type hint in method declaration, as for empty Shortcodes like [table] or [table /], an empty string is passed, see WP Core #26927.
		$shortcode_atts = (array) $shortcode_atts;

		$_render = TablePress::load_class( 'TablePress_Render', 'class-render.php', 'classes' );

		$default_shortcode_atts = $_render->get_default_render_options();
		/**
		 * Filters the available/default attributes for the [table] Shortcode.
		 *
		 * @since 1.0.0
		 *
		 * @param array $default_shortcode_atts The [table] Shortcode default attributes.
		 */
		$default_shortcode_atts = apply_filters( 'tablepress_shortcode_table_default_shortcode_atts', $default_shortcode_atts );
		// Parse Shortcode attributes, only allow those that are specified.
		$shortcode_atts = shortcode_atts( $default_shortcode_atts, $shortcode_atts ); // Optional third argument left out on purpose. Use filter in the next line instead.
		/**
		 * Filters the attributes that were passed to the [table] Shortcode.
		 *
		 * @since 1.0.0
		 *
		 * @param array $shortcode_atts The attributes passed to the [table] Shortcode.
		 */
		$shortcode_atts = apply_filters( 'tablepress_shortcode_table_shortcode_atts', $shortcode_atts );

		// Check, if a table with the given ID exists.
		$table_id = preg_replace( '/[^a-zA-Z0-9_-]/', '', $shortcode_atts['id'] );
		if ( ! TablePress::$model_table->table_exists( $table_id ) ) {
			$message = "[table &#8220;{$table_id}&#8221; not found /]<br />\n";
			/**
			 * Filters the "Table not found" message.
			 *
			 * @since 1.0.0
			 *
			 * @param string $message  The "Table not found" message.
			 * @param string $table_id The current table ID.
			 */
			$message = apply_filters( 'tablepress_table_not_found_message', $message, $table_id );
			return $message;
		}

		// Load table, with table data, options, and visibility settings.
		$table = TablePress::$model_table->load( $table_id, true, true );
		if ( is_wp_error( $table ) ) {
			$message = "[table &#8220;{$table_id}&#8221; could not be loaded /]<br />\n";
			/**
			 * Filters the "Table could not be loaded" message.
			 *
			 * @since 1.0.0
			 *
			 * @param string   $message  The "Table could not be loaded" message.
			 * @param string   $table_id The current table ID.
			 * @param WP_Error $table    The error object for the table.
			 */
			$message = apply_filters( 'tablepress_table_load_error_message', $message, $table_id, $table );
			return $message;
		}
		if ( isset( $table['is_corrupted'] ) && $table['is_corrupted'] ) {
			$message = "<div>Attention: The internal data of table &#8220;{$table_id}&#8221; is corrupted!</div>";
			/**
			 * Filters the "Table data is corrupted" message.
			 *
			 * @since 1.0.0
			 *
			 * @param string $message    The "Table data is corrupted" message.
			 * @param string $table_id   The current table ID.
			 * @param string $json_error The JSON error with information about the corrupted table.
			 */
			$message = apply_filters( 'tablepress_table_corrupted_message', $message, $table_id, $table['json_error'] );
			return $message;
		}

		/**
		 * Filters whether the "datatables_custom_commands" Shortcode parameter is disabled.
		 *
		 * By default, the "datatables_custom_commands" Shortcode parameter is disabled for security reasons.
		 *
		 * @since 1.0.0
		 *
		 * @param bool $disable Whether to disable the "datatables_custom_commands" Shortcode parameter. Default true.
		 */
		if ( ! is_null( $shortcode_atts['datatables_custom_commands'] ) && apply_filters( 'tablepress_disable_custom_commands_shortcode_parameter', true ) ) {
			$shortcode_atts['datatables_custom_commands'] = null;
		}

		// Determine options to use (if set in Shortcode, use those, otherwise use stored options, from the "Edit" screen).
		$render_options = array();
		foreach ( $shortcode_atts as $key => $value ) {
			// We have to check this, because strings 'true' or 'false' are not recognized as boolean!
			if ( is_string( $value ) && 'true' === strtolower( $value ) ) {
				$render_options[ $key ] = true;
			} elseif ( is_string( $value ) && 'false' === strtolower( $value ) ) {
				$render_options[ $key ] = false;
			} elseif ( is_null( $value ) && isset( $table['options'][ $key ] ) ) {
				$render_options[ $key ] = $table['options'][ $key ];
			} else {
				$render_options[ $key ] = $value;
			}
		}

		// Generate unique HTML ID, depending on how often this table has already been shown on this page.
		if ( ! isset( $this->shown_tables[ $table_id ] ) ) {
			$this->shown_tables[ $table_id ] = array(
				'count'     => 0,
				'instances' => array(),
			);
		}
		++$this->shown_tables[ $table_id ]['count'];
		$count = $this->shown_tables[ $table_id ]['count'];
		$render_options['html_id'] = "tablepress-{$table_id}";
		if ( $count > 1 ) {
			$render_options['html_id'] .= "-no-{$count}";
		}
		/**
		 * Filters the ID of the table HTML element.
		 *
		 * @since 1.0.0
		 *
		 * @param string $html_id  The ID of the table HTML element.
		 * @param string $table_id The current table ID.
		 * @param string $count    Number of copies of the table with this table ID on the page.
		 */
		$render_options['html_id'] = apply_filters( 'tablepress_html_id', $render_options['html_id'], $table_id, $count );

		// Generate the "Edit Table" link.
		$render_options['edit_table_url'] = '';
		/**
		 * Filters whether the "Edit" link below the table shall be shown.
		 *
		 * The "Edit" link is only shown to logged-in users who possess the necessary capability to edit the table.
		 *
		 * @since 1.0.0
		 *
		 * @param bool   $show     Whether to show the "Edit" link below the table. Default true.
		 * @param string $table_id The current table ID.
		 */
		if ( is_user_logged_in() && apply_filters( 'tablepress_edit_link_below_table', true, $table['id'] ) && current_user_can( 'tablepress_edit_table', $table['id'] ) ) {
			$render_options['edit_table_url'] = TablePress::url( array( 'action' => 'edit', 'table_id' => $table['id'] ) );
		}

		/**
		 * Filters the render options for the table.
		 *
		 * The render options are determined from the settings on a table's "Edit" screen and the Shortcode parameters.
		 *
		 * @since 1.0.0
		 *
		 * @param array $render_options The render options for the table.
		 * @param array $table          The current table.
		 */
		$render_options = apply_filters( 'tablepress_table_render_options', $render_options, $table );

		// Check if table output shall and can be loaded from the transient cache, otherwise generate the output.
		if ( $render_options['cache_table_output'] && ! is_user_logged_in() ) {
			// Hash the Render Options array to get a unique cache identifier.
			$table_hash = md5( wp_json_encode( $render_options, TABLEPRESS_JSON_OPTIONS ) );
			$transient_name = 'tablepress_' . $table_hash; // Attention: This string must not be longer than 45 characters!
			$output = get_transient( $transient_name );
			if ( false === $output || '' === $output ) {
				// Render/generate the table HTML, as it was not found in the cache.
				$_render->set_input( $table, $render_options );
				$output = $_render->get_output();
				// Save render output in a transient, set cache timeout to 24 hours.
				set_transient( $transient_name, $output, DAY_IN_SECONDS );
				// Update output caches list transient (necessary for cache invalidation upon table saving).
				$caches_list_transient_name = 'tablepress_c_' . md5( $table_id );
				$caches_list = get_transient( $caches_list_transient_name );
				if ( false === $caches_list ) {
					$caches_list = array();
				} else {
					$caches_list = (array) json_decode( $caches_list, true );
				}
				if ( ! in_array( $transient_name, $caches_list, true ) ) {
					$caches_list[] = $transient_name;
				}
				set_transient( $caches_list_transient_name, wp_json_encode( $caches_list, TABLEPRESS_JSON_OPTIONS ), 2 * DAY_IN_SECONDS );
			} else {
				/**
				 * Filters the cache hit comment message.
				 *
				 * @since 1.0.0
				 *
				 * @param string $comment The cache hit comment message.
				 */
				$output .= apply_filters( 'tablepress_cache_hit_comment', "<!-- #{$render_options['html_id']} from cache -->" );
			}
		} else {
			// Render/generate the table HTML, as no cache is to be used.
			$_render->set_input( $table, $render_options );
			$output = $_render->get_output();
		}

		// If DataTables is to be and can be used with this instance of a table, process its parameters and register the call for inclusion in the footer.
		if ( $render_options['use_datatables']
			&& $render_options['table_head']
			&& false !== strpos( $output, '<thead' ) // A `<thead>` tag is required.
			&& false === strpos( $output, ' colspan="' ) // `colspan` attributes are forbidden.
			&& false === strpos( $output, ' rowspan="' ) // `rowspan` attributes are forbidden.
			) {
			// Get options for the DataTables JavaScript library from the table's render options.
			$js_options = array();
			foreach ( array(
				'alternating_row_colors',
				'datatables_sort',
				'datatables_paginate',
				'datatables_paginate',
				'datatables_paginate_entries',
				'datatables_lengthchange',
				'datatables_filter',
				'datatables_info',
				'datatables_scrollx',
				'datatables_scrolly',
				'datatables_locale',
				'datatables_custom_commands',
			) as $option ) {
				$js_options[ $option ] = $render_options[ $option ];
			}
			/**
			 * Filters the JavaScript options for the table.
			 *
			 * The JavaScript options are determined from the settings on a table's "Edit" screen and the Shortcode parameters.
			 * They are part of the render options and can be overwritten with Shortcode parameters.
			 *
			 * @since 1.0.0
			 *
			 * @param array  $js_options     The JavaScript options for the table.
			 * @param string $table_id       The current table ID.
			 * @param array  $render_options The render options for the table.
			 */
			$js_options = apply_filters( 'tablepress_table_js_options', $js_options, $table_id, $render_options );
			$this->shown_tables[ $table_id ]['instances'][ $render_options['html_id'] ] = $js_options;
			$this->_enqueue_datatables();
		}

		// Maybe print a list of used render options.
		if ( $render_options['shortcode_debug'] && is_user_logged_in() ) {
			$output .= '<pre>' . var_export( $render_options, true ) . '</pre>';
		}

		return $output;
	}

Code file location:

tablepress/tablepress/controllers/controller-frontend.php

TablePress [table-info] Shortcode

The TablePress shortcode is a powerful tool that fetches and displays table data. . It accepts parameters like ‘id’, ‘field’, and ‘format’. The ‘id’ is used to identify the specific table, ‘field’ specifies the table data to fetch, and ‘format’ determines the data’s presentation. This shortcode is flexible, allowing for various data types like ‘name’, ‘description’, ‘last_modified’, ‘last_editor’, ‘author’, ‘number_rows’, and ‘number_columns’. It also includes filters for customizing the output.

Shortcode: [table-info]

Parameters

Here is a list of all possible table-info shortcode parameters and attributes:

  • id – The unique identifier of the table
  • field – Specifies the field to be displayed
  • format – Determines the format of the output

Examples and Usage

Basic example – Display a table using its unique ID

[table-info id="123" /]

Advanced examples

Display the name of a table using its ID

[table-info id="123" field="name" /]

Display the last time a table was modified in a human-readable format

[table-info id="123" field="last_modified" format="human" /]

Show the number of rows in a table excluding the header and footer

[table-info id="123" field="number_rows" format="raw" /]

Display the name of the last editor of a table

[table-info id="123" field="last_editor" /]

Show the author of a table

[table-info id="123" field="author" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [table-info] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( TablePress::$shortcode_info, array( $this, 'shortcode_table_info' ) );

Shortcode PHP function:

function shortcode_table_info( $shortcode_atts ) {
		// Don't use `array` type hint in method declaration, as for empty Shortcodes like [table-info] or [table-info /], an empty string is passed, see WP Core #26927.
		$shortcode_atts = (array) $shortcode_atts;

		// Parse Shortcode attributes, only allow those that are specified.
		$default_shortcode_atts = array(
			'id'     => '',
			'field'  => '',
			'format' => '',
		);
		/**
		 * Filters the available/default attributes for the [table-info] Shortcode.
		 *
		 * @since 1.0.0
		 *
		 * @param array $default_shortcode_atts The [table-info] Shortcode default attributes.
		 */
		$default_shortcode_atts = apply_filters( 'tablepress_shortcode_table_info_default_shortcode_atts', $default_shortcode_atts );
		$shortcode_atts = shortcode_atts( $default_shortcode_atts, $shortcode_atts ); // Optional third argument left out on purpose. Use filter in the next line instead.
		/**
		 * Filters the attributes that were passed to the [table-info] Shortcode.
		 *
		 * @since 1.0.0
		 *
		 * @param array $shortcode_atts The attributes passed to the [table-info] Shortcode.
		 */
		$shortcode_atts = apply_filters( 'tablepress_shortcode_table_info_shortcode_atts', $shortcode_atts );

		/**
		 * Filters whether the output of the [table-info] Shortcode is overwritten/short-circuited.
		 *
		 * @since 1.0.0
		 *
		 * @param bool|string $overwrite      Whether the [table-info] output is overwritten. Return false for the regular content, and a string to overwrite the output.
		 * @param array       $shortcode_atts The attributes passed to the [table-info] Shortcode.
		 */
		$overwrite = apply_filters( 'tablepress_shortcode_table_info_overwrite', false, $shortcode_atts );
		if ( $overwrite ) {
			return $overwrite;
		}

		// Check, if a table with the given ID exists.
		$table_id = preg_replace( '/[^a-zA-Z0-9_-]/', '', $shortcode_atts['id'] );
		if ( ! TablePress::$model_table->table_exists( $table_id ) ) {
			$message = "[table &#8220;{$table_id}&#8221; not found /]<br />\n";
			/** This filter is documented in controllers/controller-frontend.php */
			$message = apply_filters( 'tablepress_table_not_found_message', $message, $table_id );
			return $message;
		}

		// Load table, with table data, options, and visibility settings.
		$table = TablePress::$model_table->load( $table_id, true, true );
		if ( is_wp_error( $table ) ) {
			$message = "[table &#8220;{$table_id}&#8221; could not be loaded /]<br />\n";
			/** This filter is documented in controllers/controller-frontend.php */
			$message = apply_filters( 'tablepress_table_load_error_message', $message, $table_id, $table );
			return $message;
		}

		$field = preg_replace( '/[^a-z_]/', '', strtolower( $shortcode_atts['field'] ) );
		$format = preg_replace( '/[^a-z]/', '', strtolower( $shortcode_atts['format'] ) );

		// Generate output, depending on what information (field) was asked for.
		switch ( $field ) {
			case 'name':
			case 'description':
				$output = $table[ $field ];
				break;
			case 'last_modified':
				switch ( $format ) {
					case 'raw':
					case 'mysql':
						$output = $table['last_modified'];
						break;
					case 'human':
						$modified_timestamp = date_create( $table['last_modified'], wp_timezone() );
						$modified_timestamp = $modified_timestamp->getTimestamp();
						$current_timestamp = time();
						$time_diff = $current_timestamp - $modified_timestamp;
						// Time difference is only shown up to one week.
						if ( $time_diff >= 0 && $time_diff < WEEK_IN_SECONDS ) {
							$output = sprintf( __( '%s ago', 'default' ), human_time_diff( $modified_timestamp, $current_timestamp ) );
						} else {
							$output = TablePress::format_datetime( $table['last_modified'], '<br />' );
						}
						break;
					case 'date':
						$output = TablePress::format_datetime( $table['last_modified'], get_option( 'date_format' ) );
						break;
					case 'time':
						$output = TablePress::format_datetime( $table['last_modified'], get_option( 'time_format' ) );
						break;
					default:
						$output = TablePress::format_datetime( $table['last_modified'] );
						break;
				}
				break;
			case 'last_editor':
				$output = TablePress::get_user_display_name( $table['options']['last_editor'] );
				break;
			case 'author':
				$output = TablePress::get_user_display_name( $table['author'] );
				break;
			case 'number_rows':
				$output = count( $table['data'] );
				if ( 'raw' !== $format ) {
					if ( $table['options']['table_head'] ) {
						$output = $output - 1;
					}
					if ( $table['options']['table_foot'] ) {
						$output = $output - 1;
					}
				}
				break;
			case 'number_columns':
				$output = count( $table['data'][0] );
				break;
			default:
				$output = "[table-info field &#8220;{$field}&#8221; not found in table &#8220;{$table_id}&#8221; /]<br />\n";
				/**
				 * Filters the "table info field not found" message.
				 *
				 * @since 1.0.0
				 *
				 * @param string $output The "table info field not found" message.
				 * @param array  $table  The current table ID.
				 * @param string $field  The field that was not found.
				 * @param string $format The return format for the field.
				 */
				$output = apply_filters( 'tablepress_table_info_not_found_message', $output, $table, $field, $format );
		}

		/**
		 * Filters the output of the [table-info] Shortcode.
		 *
		 * @since 1.0.0
		 *
		 * @param string $output         The output of the [table-info] Shortcode.
		 * @param array  $table          The current table.
		 * @param array  $shortcode_atts The attributes passed to the [table-info] Shortcode.
		 */
		$output = apply_filters( 'tablepress_shortcode_table_info_output', $output, $table, $shortcode_atts );
		return $output;
	}

Code file location:

tablepress/tablepress/controllers/controller-frontend.php

Conclusion

Now that you’ve learned how to embed the TablePress – Tables in WordPress made easy Plugin shortcodes, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *