最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to send a mail when a plugin update found?

programmeradmin8浏览0评论

Is there any hook for when any plugin update is found to trigger a mail using PHP? I found reference to upgrader_process_complete hook, but that is called after plugins are updated and not before.

Also, how can I fetch the list of plugins which have updates pending at a given time?

Is there any hook for when any plugin update is found to trigger a mail using PHP? I found reference to upgrader_process_complete hook, but that is called after plugins are updated and not before.

Also, how can I fetch the list of plugins which have updates pending at a given time?

Share Improve this question asked May 7, 2020 at 10:39 Saeesh TendulkarSaeesh Tendulkar 1531 silver badge7 bronze badges 2
  • You're going to need to track which plugins you've sent emails for and which versions, otherwise you're going to recieve a lot of emails – Tom J Nowell Commented May 7, 2020 at 11:25
  • @TomJNowell Right. That can be done by storing in database. But how do I fetch the plugins which has updates available? – Saeesh Tendulkar Commented May 7, 2020 at 11:32
Add a comment  | 

2 Answers 2

Reset to default 1

The relevant code is wp_update_plugins(). You can see that it stores its state in a site transient called 'update_plugins':

set_site_transient( 'update_plugins', $new_option );

and we can hook that:

  • pre_set_site_transient_update_plugins - called at the start of set_site_transient()
  • set_site_transient_update_plugins - called at the end, if the value was changed

Note that

  • set_site_transient( 'update_plugins', ... ) is called twice as part of the update: once to update the last_checked timestamp, and once with the new-plugins-to-update list; the first call will always therefore have a changed value without necessarily having the list of updated plugins changed
  • it can also be called when deleting plugins, to remove the update flag for any deleted plugin, but only once in this case.

I appreciate that hooking the site transient updates seems a bit of a hack, but there's no other obvious place to hook that the update check is complete. I have seen pay-for plugins hook these too as a way of generating update-needed for plugins hosted elsewhere.


Alternatively of course you don't need to hook the actual update, but you can instead schedule a cron job that checks get_site_transient( 'update_plugins' ) (and update_themes and update_core) to generate the notification email.

With the answer of Rup, you can create a simple mail with the wp_mail function.

It would look like this:

 wp_mail( get_bloginfo( 'admin_email' ), 'Subject title here', 'Text for mail here'); 

With get_bloginfo( 'admin_email' ) you'll retrieve the admin's email you filled in during installation.

发布评论

评论列表(0)

  1. 暂无评论