admin管理员组文章数量:1126350
i have sample wordpress plugin class. Im hooking woocommerce woocommerce_saved_order_items
and i want create a admin notice. Add action from __construct works, but when i want create a notice in hook function it doesnt appears, where is the problem?
class SomeClass {
private static $instance;
public function __construct() {
add_action('woocommerce_saved_order_items',array($this,'orderStatusChange'),10,1);
add_action('admin_notices', array($this,'simple_notice')); // This works
}
public static function getInstance() {
if(!self::$instance)
self::$instance = new SomeClass();
return self::$instance;
}
public function orderStatusChange($orderID){
add_action('admin_notices', 'simple_notice');//This not works
}
function simple_notice(){
?>
<div class="updated notice is-dismissible">
<p>Thank you for using this plugin! <strong>You are awesome</strong>.</p>
</div>
<?php
}
}
SomeClass::getInstance();
i have sample wordpress plugin class. Im hooking woocommerce woocommerce_saved_order_items
and i want create a admin notice. Add action from __construct works, but when i want create a notice in hook function it doesnt appears, where is the problem?
class SomeClass {
private static $instance;
public function __construct() {
add_action('woocommerce_saved_order_items',array($this,'orderStatusChange'),10,1);
add_action('admin_notices', array($this,'simple_notice')); // This works
}
public static function getInstance() {
if(!self::$instance)
self::$instance = new SomeClass();
return self::$instance;
}
public function orderStatusChange($orderID){
add_action('admin_notices', 'simple_notice');//This not works
}
function simple_notice(){
?>
<div class="updated notice is-dismissible">
<p>Thank you for using this plugin! <strong>You are awesome</strong>.</p>
</div>
<?php
}
}
SomeClass::getInstance();
Share
Improve this question
asked Feb 20, 2017 at 15:44
BullmanBullman
1612 bronze badges
1
- 1 Are you found any solution about this? I have the same problem! – Sangar82 Commented Aug 28, 2017 at 10:46
1 Answer
Reset to default 1Your problem is pretty simple. Your callback for this hook is not a simple function but some method of a class.
If you add action like this:
add_action('admin_notices', 'simple_notice');
you tell WP that there is some simple function called simple_notice
and it should be called when hook admin_notices
is processed. But... There is no such function anywhere in your code.
The function you want to call is method in your class, so you have to pass not only the name of function, but also object of given class, so WP is able to call this method. (And you've done it correctly in __construct).
This line works
add_action('admin_notices', array($this, 'simple_notice')); // This works
because the method is passed with full details - as an array describing object and it's method to call.
本文标签: Wordpress admin notice in plugin function
版权声明:本文标题:Wordpress admin notice in plugin function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736683687a1947553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论