2

I'm using a plugin that doesn't provide many hooks for its actions. I would like to be able to hook in when the plugin adds a row to a plugin-specific table, and update some other data elsewhere. (For context, it's a CRM. Whenever a log is added to its logs table, I would also like to update the CRM users table, updating the status column for the user associated to the log.)

I know there are hooks like pre_get_posts to modify a main query. However, since the insert isn't a get_posts() query, I am looking for something similar that fires when data is inserted, and then I'll narrow it down to only when data is inserted into this particular table.

2
  • Is the plugin properly using the wpdb class for DB interaction?
    – kraftner
    Jul 27 at 15:38
  • Yes, it's using $wpdb->update / $wpdb->insert to add the log.
    – WebElaine
    Jul 27 at 15:58

2 Answers 2

5

If you have access to the database, you could potentially create a trigger for that plugin-specific table. This bypasses the need for a hook. Depending on how they handle database schema updates, it might even stick around after a plugin update. I've never tried triggers on WP tables though, so take this with a grain of salt.

E.g.

CREATE TRIGGER after_customer_insert
AFTER INSERT
ON wp_plugin_customers FOR EACH ROW
BEGIN
    IF NEW.PhoneNumber IS NOT NULL THEN
        UPDATE wp_plugin_other_table SET column = NEW.PhoneNumber
        WHERE other_column = NEW.CustomerCompany;
    END IF;
END

Edit: You could also ask the plugin author about adding a hook. Sometimes they oblige.

1
  • Very interesting! I haven't seen anything like that before. Sounds like it's worth installing an older plugin version, adding the trigger, then updating the plugin to make sure it stays in place. I'll definitely log an issue with the developer to hopefully add a hook, but likely need to get something in place before they make that update.
    – WebElaine
    Jul 27 at 17:55
4

As you probably already noticed yourself $wpdb doesn't have any hooks that you can use.

The answer @brennan gave already sounds like an interesting approach, but probably won't help if you have to solve it in code without access to the DB to create the trigger.

I have another idea, that although hacky, might also work depending on what exactly your requirements are.

So you said "[…] able to hook in when the plugin adds a row to a plugin-specific table […]". So I assume you really only are talking about adding rows, not editing existing. I am also assuming that the table has some auto incrementing ID.

If that is the case what about this approach:

  1. Store the highest ID of a row in the relevant table, e.g. in an option.
  2. Check if the stored ID is lower than the current highest ID.
  3. Get all rows from the last stored ID to the current ID.
  4. Do whatever you need to do with that information.

When you do this really depends on the exact requirements of your "update some other data elsewhere". You could do it in the shutdown hook, in CRON that runs every couple minutes, some hook from the plugin that reliably runs after updates or - thinking about your "updating the status column for the user associated to the log" - right before the reading of the user status.

EDIT: the comment by @birgire just made me realize another thing: You can try hooking to query to set a flag if the query is the one you care about and only do the check from above if that flag is set to true. This should be way better from a performance perspective.

I am intentionally leaving out any specific code, but I hope the approach is clear.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.