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