6

I'm coding a plugin which probably will have updates in the future, and I want to take advantage of the automatic update mechanism in wordpress.

I followed this tutorial:

Everything seemed quite straightforward to me, but for some reason, nothing works. The function hooked to the pre_set_site_transient_update_plugins filter never gets called.

I tried to go step by step and just added a "Hello" and a log line on that hook. It simply doesn't get called, even when I tried to force the update checks.

Is there any catch on that? Btw, I'm trying that on a multisite installation.

Any help would be greatly appreciated.

Arnaldo

UPDATE: I got the function hooked to the pre_set_site_transient_update_plugins to be called. Strangely, if I do the hooking inside the constructor of the object which has the checking function it works, otherwise, it doesn't.

Example (this works):

class XYZ {
    public function __construct() {
        add_filter('pre_set_site_transient_update_plugins', array($this, 'check_update'));
    }

    public function check_update($transient) {
        // logic here
    }
}

However, if I simply do this on my main plugin file, it doesn't work:

add_filter('pre_set_site_transient_update_plugins', array('XYZ', 'check_update')); 

First of all, I'd like to understand what's the difference between the two scenarios. Besides that, I had to do

set_site_transient('update_plugins', null);

to force the hook to be called, otherwise I think I'd have to wait the normal wordpress update check cycle, right? And now, another issue has surfaced: the $transient variable which is passed to the check_update() function is always null! Is that because of the set_site_transient() instruction? If it is, how can I check the whole solution without suffering for several hours to be able to test my latest changes?

Thanks again, Arnaldo

UPDATE 2: @kaiser, the behavior is exactly as I described, even using a static method. I had tried that already.

2
  • 2
    Please narrow code down enough to just demonstrate the issue and add it to the question.
    – Rarst
    Commented Apr 18, 2014 at 20:21
  • 1
    From your update: Try public static function check_update().
    – kaiser
    Commented Apr 19, 2014 at 1:36

3 Answers 3

4

You actually have several questions in there, so I'll answer them one by one:

However, if I simply do this on my main plugin file, it doesn't work:

add_filter('pre_set_site_transient_update_plugins', array('XYZ', 'check_update'));

First of all, I'd like to understand what's the difference between the two scenarios.

This is failing because you are calling the method statically, instead of on an instance of the class. Since the method isn't being called on an instance, the class properties aren't set. So for example, when check_update() tried to use $this->current_version, it will get null. For this reason the function will assume that there is no update available.

Besides that, I had to do

set_site_transient('update_plugins', null);

to force the hook to be called, otherwise I think I'd have to wait the normal wordpress update check cycle, right?

Yes.

And now, another issue has surfaced: the $transient variable which is passed to the check_update() function is always null! Is that because of the set_site_transient() instruction?

Yes, you are setting the transient to null. So, you shouldn't expect it to be anything but null afterward, unless that failed for some reason.

[H]ow can I check the whole solution without suffering for several hours to be able to test my latest changes?

For testing, change it to hook into get_site_transient_update_plugins instead. That will get called each time the transient is retrieved. You may want to set a static variable or something though, so you don't call it more than once per page-request.

1
  • Another option to force updates is wp_maybe_auto_update()
    – Howdy_McGee
    Commented Apr 1, 2015 at 17:24
2

This might not solve your problem directly, but I would like to mention that if you want to avoid WordPress.org you can host your plugin on GitHub (or Bitbucket).

There is a updater plugin for that and on Tut+ is a good Tutorial for that: Distributing Your Plugins in GitHub with Automatic Updates

0

Just use this:

add_filter('site_transient_update_plugins', array($this, 'check_update'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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