2

I'm using TablePress to make some backend tables. What I want to do is show these tables in a different admin menu than the TablePress menu, because they'll be available to lower user groups.

So I found out how to create a custom menu and in that menu I have custom pages. In these pages I would like to show the shortcode [table id=1 /] (a table that has already been created).

I've tried

echo do_shortcode('[table id=1 /]');

but that just shows [table id=1 /].

The plugin uses

add_shortcode( 'table', array( $this, 'shortcode_table' ) );

so I can't use directly the shortcode callback because I've no access to $this variable outside the object (the object class is TablePress_Frontend_Controller).

In addition, looking at plugin files, I've noticed one called template-tag-functions.php that contains a function tablepress_print_table. However this function, just like shortcode, works on frontend but not on backend (throwing an error).

What is the best way to solve this?

  • I've read through that question and its answers and tried to duplicate it using a fuction from tablepress, but it didn't work. – Fid Jan 9 '14 at 1:13
  • Tried it upon reading your comment and I get Fatal error: Call to undefined function. Should I find and copy that function over to my plugin functions? – Fid Jan 9 '14 at 1:58
  • Almost there Catchable fatal error: Argument 1 passed to TablePress_Frontend_Controller::shortcode_table() must be an array, string given – Fid Jan 9 '14 at 2:24
  • 1
    Awesome! It shows the table headings and rows, though unformatted (background etc.), but hopefully I should be able to figure that out. – Fid Jan 9 '14 at 2:41
  • Following up on this, adding css and stuff was an easy task, but I now would also like to add datatables_tabletools="true" to the 'admin shortcode'. I know it'll also need the js loaded, that's no problem. – Fid Feb 20 '14 at 2:25
3

As you noticed, the plugin use an instance of the class TablePress_Frontend_Controller to render the shortcode.

This instance is created by only on frontend requests, this is the reason why shortcode doesn't work on backend.

The function tablepress_print_table function use the TablePress_Frontend_Controller::shortcode_table method to render the table, and this is the reason why also that function doesn't work.

So what you need is create an instance of that class and call shortcode_table method. However to do this, you need to load the required files and setup class dependencies, but luckely the plugin main class 'TablePress' has a static method that do all the hard work: load_controller.

You can use it to write a custom function and put it in your plugin or theme functions.php

function tablepress_backend_table( $id = '' ) {
  if ( empty($id) ) return;
  if ( class_exists('TablePress') ) {
    $c = TablePress::load_controller( 'frontend' );
    echo $c->shortcode_table( array('id' => $id ) );
  }
}

after that where you need you can print the wanted able like so:

// print the table with ID = '1'
tablepress_backend_table( '1' );

However, that will print the table without styling. In fact, the plugin doesn't add css on backend, so if you want your tables display styled you need to enqueue plugin styles by yourself (.css files are in the 'css' subfolder of plugin). For that pourpose be sure to use wp_enqueue_style function hooked into admin_enqueue_scripts hook and use some logic to enqueue the styles only in the specific admin pages where you need them.

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.