WordPress.org

Forums

TablePress
[resolved] hook / action when saving or updating a table (14 posts)

  1. Andrew Norcross
    Member
    Posted 1 year ago #

    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?

    https://wordpress.org/plugins/tablepress/

  2. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    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 Type tablepress_table. It therefore also triggers save_post, I'm pretty sure.

    Regards,
    Tobias

  3. Andrew Norcross
    Member
    Posted 1 year ago #

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

  4. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    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 the tablepress_table Custom Post Type.

    You will just have to remember to hook into save_post early enough.

    Regards,
    Tobias

  5. Andrew Norcross
    Member
    Posted 1 year ago #

    my mistake. while save_post is 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.

  6. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    no, as this is called in the same way as with other post types, this data is being passed to the save_post hook. Did you maybe forget the $accepted_args parameter in the add_action() call, like

    add_action( 'save_post', 'your_action_handler', 10, 3 );

    ?

    Catching this in the admin_init hook is not a good idea, as this might potentially change in later TablePress versions.

    Regards,
    Tobias

  7. Andrew Norcross
    Member
    Posted 1 year ago #

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

  8. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    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" the save_post plugin action hook that is fired in the wp_insert_post() WordPress function (see https://core.trac.wordpress.org/browser/trunk/src/wp-includes/post.php#L2970 )...

    Regards,
    Tobias

  9. Andrew Norcross
    Member
    Posted 1 year ago #

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

  10. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    you should not check for the $_POST data here, but only for the data that is run through the save_post action. With that, you will also catch changes to TablePress tables.

    Regards,
    Tobias

  11. Andrew Norcross
    Member
    Posted 1 year ago #

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

  12. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    but you are getting that data as part of the $post parameter 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,
    Tobias

  13. Andrew Norcross
    Member
    Posted 1 year ago #

    actually, 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!

  14. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    no problem, you are very welcome! :-) Always glad when I can help!

    Best wishes,
    Tobias

    P.S.: In case you haven't, please rate TablePress here in the plugin directory. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • TablePress
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic