Programmatically add new table
-
I want to add tables programmatically along with the data. Can you kindly give me a code snippet to do so? The final output should be the tableID which I can use to add into posts.
-
Hi,
thanks for your question.
TablePress does not have an API for programmatically adding tables, as it is not designed to be a database system. You could however use the code in the function
handle_post_action_add()in the /controllers/controller-admin.php file.Also, if you explain what you are trying to do, I might be able to find a better method for this.
Regards,
TobiasHi Tobias,
Thanks for your response. Let me explain what I want exactly.
I want to have weekly box office stats e.g. http://www.imdb.com/chart/. Now each movie data is fetched programmatically. These movie data should be inserted into a tablepress table row. The resulting table will be inserted into a post as a shortcode and published.
Pseudo code:
function add_tablepress_table($data) { $table_name = 'week of 23 August 2013'; $table_desc = 'Box Office result for week of 23 August 2013'; $table_id = tablepress_insert_table($table_name, $table_desc, $default_table_options); foreach($data as $row) { $table_id->set_row($row['movie_rank'], $row['movie_name'], $row['weekend'], $row['gross']) } return $table_id; }This $table_id will be used to insert into a post. Does that make sense?
Hi,
ok, thanks for the explanation!
This looks as you want to use TablePress in a database fashion. This is however not really a good idea, as that’s not what TablePress was designed for.
Instead, I recommend to work with custom mySQL tables here, where your custom PHP code would store the movie data. Then, you could create your own Shortcode to render the table HTML from that and display it, and style it with CSS. If you then want the extra features like sorting and search, you can add the external DataTables JS library from http://www.datatables.net manually.
Regards,
TobiasHi Tobias,
In that case, it will be almost a complete rework of what is already available in your plugin. Moreover the tables won’t be only added programmatically. Existing data will be added programmatically and new data will be loaded normally using the UI.
If you could kindly give a code snippet to add a new table with default table options, I can do the rest of adding rows. I’ve checked how table data is stored as post content. The only thing that bothers me is inserting the metadata “_tablepress_table_options” and “_tablepress_table_visibility”.
Even that can be done, but I always try to use native plugin functions because it will take care of inserting other required metadata.
Thanks,
Prasenjit.Hi,
ok, I see… Then, instead of adding them programmatically, you can maybe use the HTML import? Just to give you more ideas…
Regardless, I already mentioned the necessary function: Please see
handle_post_action_add()in the /controllers/controller-admin.php. That shows you how TablePress adds a new table.Regards,
TobiasNevermind Tobias. I switched to Footable plugin. That suits my requirements better than yours. Keep up the good work. You’ve created an excellent plugin.
Hi,
no problem! Good to hear that you found a solution to your idea! 🙂
Best wishes,
Tobias
The topic ‘Programmatically add new table’ is closed to new replies.