admin管理员组文章数量:1410674
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.
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.
Share Improve this question edited Apr 19, 2014 at 6:01 Pieter Goosen 55.4k23 gold badges116 silver badges210 bronze badges asked Apr 18, 2014 at 19:01 arnaldo2204arnaldo2204 913 silver badges5 bronze badges 2 |3 Answers
Reset to default 4You 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 thecheck_update()
function is alwaysnull
! Is that because of theset_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.
This might not solve your problem directly, but I would like to mention that if you want to avoid WordPress 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
Just use this:
add_filter('site_transient_update_plugins', array($this, 'check_update'));
本文标签: Automatic updates in pluginnot hosted on wordpress repository
版权声明:本文标题:Automatic updates in plugin - not hosted on wordpress repository 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744997334a2636758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
public static function check_update()
. – kaiser Commented Apr 19, 2014 at 1:36