This post in my WP-Table Reloaded Extension series should be very useful to a lot of people, taking into consideration the questions I receive on this. Using the code from below, you will extend WP-Table Reloaded so that PHP code in table cells will be parsed and executed. This is useful for dynamic content generation or integration of other plugins’ features.
To get started with WP-Table Reloaded Extensions, you should read the introduction and follow the included instructions. You will also find links to the other presented Extensions there.
Security Warning
Before I start, let me explain, why you should only use this Extension, if you are really sure that you need it and if you really know what you are doing: PHP code is very mighty. Due to the structure of PHP and the HTTP request handling, code that is run from within the table lives in the same “area” as the WordPress (or plugin) PHP code and thus has the same rights and possibilities. However, it is not solely controlled by the admin as the plugin or WP core files usually are.
This is especially critical if you have multiple users registered in your WordPress site: Anyone who can edit tables of WP-Table Reloaded will be able to insert (and with that cause execution of) PHP code. And with that, he can do a lot of bad things, if he wanted! What this means: If you want to use this Extension, make sure that only people who you trust can edit tables in WP-Table Reloaded (using the corresponding setting in the “Plugin Options”). Preferably this should be only admins of your site. Again: Treat this step seriously! Neither WP-Table Reloaded nor the Extension do any checks on whether a user has the right to insert PHP code! Again: If a user can edit tables, he will be able to enter any PHP code he wants! (Obviously, all of this is no problem, if you are the only registered person in your WordPress site.)
I will not take any responsibility if this feature is misused!
If you can, you should avoid using this Extension, i.e. by developing your own Shortcode (remember: Those also work in tables!) as you’ll have control of the underlaying PHP code and not the user who edits a table.
For the PHP developers: The Extension uses PHP’s eval() function. That function takes a string (in our case the cell content) and executes it as PHP. Due to the internal structure of WordPress Shortcodes, we need some output buffering around that function, so that any outputs will not be sent to the browser directly, but together with the output of the [table id=N /] Shortcode. (Take this into consideration before using the Extension, as output buffering on a large number of cells might slighty increase the time needed to render a table and the load on your webspace or server.)
Additionally, on certain hosts, the eval() function might be disabled due to security reasons.
Now lets come to the actual code of the Extension:
/** * Executes PHP code in table cells * @author Tobias Baethge * @see http://tobias.baethge.com/2010/02/extension-5-how-to-use-php-in-table-cells/ */ function wp_table_reloaded_execute_php_in_cells( $cell_content ) { ob_start(); eval( '?>' . $cell_content ); $output = ob_get_clean(); return $output; } add_filter( 'wp_table_reloaded_cell_content', 'wp_table_reloaded_execute_php_in_cells' ); |
The code form above just needs to be copied into the file “wp-table-reloaded-extensions.php”, created according to these instructions (after the Plugin Header comment, but before the closing PHP bracket ?>). Then, just activate the new plugin “WP-Table Reloaded Extensions”, if you haven’t. That’s it! :-)
To use PHP in a table, you can now enter commands into your table cells. They have to be valid PHP syntax, including the opening and closing brackets (<?php and ?>). In those commands, you can use the regular echo function to output text. Any text that is not within the brackets will be printed as-is.
If you like this series of Extensions, I’m happy about any feedback, and especially about further suggestions!
Previous posts in this series that might interest you:


When I try to use this extension, I get an error – “Fatal error: Call to a member function attributes() on a non-object in /usr/home/…/wp-content/plugins/wp-table-reloaded-extensions.php(16) : eval()’d code on line 1″
Hi,
you get that error because there is an error in the code that you are trying to execute :-) That’s what the “eval()’d code on line 1″ means. There is no error here in WP-Table Reloaded or in the Extension, as neither of them have such an attributes() function.
So, you might want to check your code again. :-)
Best wishes,
Tobias
Hi Tobias, thanks for the plugin extensions!
Question: Will there be any problems with your extensions when updating WP-Table Reloaded?
Hi,
thanks for your question. No, there will not be any problems, because the extensions and the plugin are independent from each other. In fact, this is actually the reason, why I came up with the idea of extensions: With them, there is no need modify the plugin files, because those modifications would be lost during an update. You must, however, keep the extensions in their own folder, and not in the “wp-table-reloaded” plugin folder.
Best wishes,
Tobias
I went ahead and created the extension plugin via your instructions. I then activated it via the admin menu.
Then, I put the following php code
[stripped]
into a cell to pull a custom field out of a post and all it did was print this in the cell box:
Did I miss something here or screw it up? Any thoughts?
My overall goal is to have a table on a page that will pull in the custom fields out of posts (using flutter to create custom fields in the admin part of the post). You then could click on that project (a link) to go to the actual post which would give more information on the project.
Does that make sense? Any thoughts or help in pointing me in the right direction?
Thanks in advance. Love the plugin by the way. Very useful.
Hi Jeremy,
you need to use full PHP code, like:
(Maybe you did, I can’t tell, because WordPress stripped your first code snippet.)
If you did actually type that, it is possible that your server prevents text fields from having that content. If that is the case, you might try importing a pre-filled table.
Regards,
Tobias
I went ahead and used your above code, double-checked it a million times, and nothing will show up in the activated or deactivated plugins.
Am I doing something wrong?
And are there any compatibility issues with WordPress 3.0.1?
Hi Michael,
thanks for your comment.
Does your file contain a correct plugin header. Where did you save it?
There are no known compatibility issues with WordPress 3.0.1.
Best wishes,
Tobias
Thanks man, great work. You are a deity ;-) Extension working fine here, it was the one function I was looking for in a complete table plugin. Thanks for posting this und tschüss, but I’ll be coming back here for sure…
Great extension! Thank you so much. I use this with the PHP Exec plugin for WordPress and have some problems executing PHP in table fields… but i’m pretty sure the error is on my side… thank in any case!
Great extension, really good.
I’m trying to get the extension to return some data about posts. I’d love to be able to list the most recent posts, but I can’t even get the most basic WordPress data to return (e.g. the current page title).
Any ideas? Some examples would be most appreciated.
Thanks in advance!
Hi Tim,
thanks for your comment. Unfortunately, I don’t know the WordPress post retrieval functions (aka “The query” or “The Loop”) well enough to be of any help with them (I’m not a theme developer). You should take a look at the Template Tag functions for the posts, e.g. in the WordPress Codex.
Having said that, a remark: If you want to show post data in tabular form, it is usually much easier to build your own table in “The Loop”, instead of trying to work with PHP code in table cells.
Regards,
Tobias
Hi Tobias,
Thanks for the reply. The sad thing is I’d love to be able to utilise all the cool functionality of the WP-Table Reloaded plugin. If I were to build a table myself, and I’m sure I could in time, I’d have to figure out how to tie in all the other bits.
Maybe this is just something I’ll have to learn. Having played around with WP-Table Reloaded for a while now, I’m starting to see how it could be problematic to get it working from within cells alone.
Great plugin all the same though, and I wish you all the best.
Cheers,
Tim
Hi Tim,
you can actually use those features (like sorting, pagination, and search) on your own HTML tables, by using the same script that WP-Table Reloaded uses: The great DataTables JavaScript library from http://www.datatables.net
Best wishes,
Tobias
Great extension for the WP-Table Reloaded plugin! By far the best table plugin for WordPress out there. And by far the best documentation! Man, this is also fine stuff for your CV, isn’t it…?! I mean, I’d hire you, that’s for sure, when seeing this. thx.
Works perfectly! Great work.
Tobias, Bravo!
I have, however, run into a bit of a problem.
I have this plugin installed as follows:
(1) domain:wordpress
and
(2) domain:subdomain:wordpress
Everything installed and working fine in (1) with one simple 4 cell table in use.
Everything installed and back end looked good while setting up new tables x13. I was able to go in and even make table name changes, etc. Then something happened: All of a sudden every time I go to “update” I get a screen with “Are you sure you want to do this?” and that is it! Sometimes it also displays, a “try again” message, it takes me back to the page with no changes that I made.
I am by no means a programmer, barely a web designer! Any chance you could give me a hand resolving this problem? I love this idea, I want to use it for a store configuration/layout guide. But now I am stuck.
Regards,
Bill
P.S.:
I should add, that I have tried simply deactivating the plugin and reactivating, which resulted in no change.
Also I deactivated, uninstalled, and then reinstalled, which also resulted in no change of the problem.
Interestingly, after uninstalling, and reinstalling all my tables still existed. I was not necessarily expecting that outcome.
Just thought you would want to know what I had tried.
Hi Bill,
thanks a lot for your comment and the detailed description. Unforunately, this seems to be a problem with your WordPress configuration, so it is not really related with the plugin. Are you using WordPress MultiSite? And are your URLs configured correctly on the “General Settings” tab of WordPress? Unfortunately, I can’t really help with that, as I have no knowledge about it. So, you might want to try the WordPress Support Forums with this, as it is a WordPress problem and not a WP-Table Reloaded problem.
Oh, and the data was still there despite uninstalling, because you had not checked the corresponding option on the “Plugin Options” screen.
Regards,
Tobias
Hi,
I can write now
[stripped by WordPress]
but if i write
[stripped by WordPress]
the table is empty!
Regards,
Matthias
Sorry, but your comment deleted my text:
Variables with $, I can’t show in the table.
Hi Matthias,
thanks for your comment. You are right, my WordPress stripped the code from your comment, for security reasons.
I assume that this is the same question as in the Forums, right?
If yes, I will answer there.
Best wishes,
Tobias