Hi,
thanks for your post, and sorry for the trouble.
The relationship between table ID and post ID is stored in the tablepress_tables option in the wp_options database table, i.e. you could use get_option() for this. That said, I really don’t recommend that.
Instead, for modifying tables externally, use the TablePress PHP functions, like
$table_id = '123';
// Load table.
$table = TablePress::$model_table->load( $table_id );
if ( is_wp_error( $table ) ) {
// Error!
return;
}
$modified_table = $table;
// Do your modifications in $modified_table now.
// Check consistency of new table, and then merge with existing table.
$table = TablePress::$model_table->prepare_table( $table, $modified_table, true );
if ( is_wp_error( $table ) ) {
// Error!
return;
}
// Save updated table.
$saved = TablePress::$model_table->save( $table );
if ( is_wp_error( $saved ) ) {
// Error!
return;
}
Out of curiosity: What do you want to modify in the tables?
Regards,
Tobias
Thread Starter
grex22
(@grex22)
Oh wow, of course there’d be a nice helper function to modify table data! Thanks for this.
Our use case is we’re pulling in some rows of data from an API and need to replace the data in a TablePress table with it. The old code we’re running takes the incoming data, formats it in the format TablePress expects as one giant string, and writes it to the wp_post that the Table is tied to.
Unsurprisingly, it sounds like there’s a better way 🙂
In your very helpful code you provided, at a high-level what does modifying the $table look like? Is $table just an array of rows? So changing $modified_table, checking for consistency, then saving it is basically a couple of simple operations on a big ol’ PHP array?
If so, this is going to simplify our old code SO much..
Thread Starter
grex22
(@grex22)
As a follow-up, I’ve been reviewing the source code for “prepare_table”, and it’s not entirely clear to me what the usage would look like if I wanted to completely replace the first table with the second. The comments in the function talk of merging, but in my use case I want to completely overwrite the contents of the original table with the modified one.
Hi,
in principle, the $table variable in that example will have this structure: https://github.com/TablePress/TablePress/blob/fa467e44056a6f42f7e955e32a753413c2740689/models/model-table.php#L804-L837
The table data is in the $table['data'] array element, which is just an array of rows (which themselves are an array of cells for that row).
Calling prepare_table() is indeed not strictly necessary. It can essentially serve as a safe guard that checks if everything is complete in the table.
Regards,
Tobias
P.S.: Depending on how that API looks like, you might also be able to use the Automatic Periodic Table Import Premium module for this.
Thread Starter
grex22
(@grex22)
Fantastic info, thank you Tobias! I don’t believe the Automatic Import plugin would be a good fit for this based on how the Salesforce authentication is setup, but good to know that exists for future projects!
Re: the original quesiton on the table structure, so essentially I could replace the $table[‘data’] multidimensional array with my new data, check it with prepare_table for sanity, and then call the save method. Seems pretty simple?
Hi,
correct. One thing to keep in mind: If the new table has more/fewer rows/columns, you must also adjust the size of the respective $table['visibility'] array.
And you could probably set the third parameter ($table_size_check) of the prepare_table() call to false then, as you can probably trust your modification 🙂
Regards,
Tobias
Thread Starter
grex22
(@grex22)
Amazing! Thank you Tobias
Hi,
no problem, you are very welcome! 🙂 Good to hear that this helped!
Best wishes,
Tobias