hook / action when saving or updating a table
-
I have built a custom plugin for a client that notifies a third party (via email) when changes to content are made on the site. The site is also using TablePress, which doesn’t use the native save_post hook for content, therefore my normal functions aren’t working. I’ve dug through the source code, but can’t find a hook or action that fires when a table is made or update. is there one?
-
Hi,
thanks for your post, and sorry for the trouble.
TablePress uses
wp_insert_post()internally, as the table data is stored as a Custom Post Typetablepress_table. It therefore also triggerssave_post, I’m pretty sure.Regards,
TobiasI can see the internal method, but since it’s being called via admin-ajax, the normal save_post doesn’t ever get registered or loaded (at least none of my testing so far has shown that to be the case). since you’re passing the table ID and not the post ID, it wouldn’t function properly anyways in that regard.
so is there a hook or function I can latch on to? I can most likely jump in with an admin_init looking for the ajax call, but I figured I would check first.
Hi,
no,
wp_insert_post()is a low-level API function. It doesn’t matter whether it’s called from admin-ajax or not. Also, it does not get the table ID, but the post ID of the post with thetablepress_tableCustom Post Type.You will just have to remember to hook into
save_postearly enough.Regards,
Tobiasmy mistake. while
save_postis getting called, none of the normal data available during that function is available (post ID, post status, etc) since you’re determining that information later down the line.I’ll just catch the ajax call using
admin_init. a bit hacky, but it’ll work.Hi,
no, as this is called in the same way as with other post types, this data is being passed to the
save_posthook. Did you maybe forget the$accepted_argsparameter in theadd_action()call, likeadd_action( 'save_post', 'your_action_handler', 10, 3 );?
Catching this in the
admin_inithook is not a good idea, as this might potentially change in later TablePress versions.Regards,
Tobiasok then, what are the additional accepted args? when looking through Firebug (or Fiddler), these are the only args being passed:
_ajax_nonce action tablepress[data] tablepress[description] tablepress[id] tablepress[name] tablepress[new_id] tablepress[number][columns] tablepress[number][hidden_rows] tablepress[number][hidden_columns] tablepress[number][rows] tablepress[options]nowhere in that do I have any of the underlying post data. the function I wrote takes that name and then finds the post ID, adds some data I need, and goes forward. what do you recommend? in specific, I need the post type, ID, status, and the user who made the change.
Hi,
ok, now I’m confused. This is a list of the HTTP POST query parameters that are transmitted when a table is saved.
To me, it sounds as if you are “investigating” thesave_postplugin action hook that is fired in thewp_insert_post()WordPress function (see https://core.trac.ww.wp.xz.cn/browser/trunk/src/wp-includes/post.php#L2970 )…Regards,
Tobiasthat is correct, I absolutely am. the plugin I’m building is for a financial firm that has to notify their compliance department whenever changes are made,so what I’ve build gets the user ID that made the change (via the $_POST data) and then other info (title, modified time, etc) from the data in the post table.
Hi,
you should not check for the $_POST data here, but only for the data that is run through the
save_postaction. With that, you will also catch changes to TablePress tables.Regards,
Tobiasin my case that won’t work, because I need to check the post type and status, as those are filterable for user preference (choose what types they want to be notified about, what status, etc).
the admin_init check does what I want. it’ll have to do for now. thanks for your help.
Hi,
but you are getting that data as part of the
$postparameter in that action hook.
I really recommend using that, as you won’t even have to care about adding notification support for other plugins that. All plugins that somehow deal with posts (or use posts to store data, like TablePress does), and that use the WordPress API, will trigger this action when editing/saving a post (or table).Regards,
Tobiasactually, you just gave me an idea. it’ll involve changing the checks my plugin does, but it’ll do the job without having to use admin_init. thanks!
Hi,
no problem, you are very welcome! 🙂 Always glad when I can help!
Best wishes,
TobiasP.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!
The topic ‘hook / action when saving or updating a table’ is closed to new replies.