admin管理员组文章数量:1130349
I'm building a plugin which generates custom css, I have 2 classes:
- The main class, with core functions
- The compiler class
as following (I simplify everything, because there is a lot of options):
class compiler
{
public function __construct(){
$this->generateCss();
$this->enqueueCss();
//other functions
}
public function generateCss(){
//this is the filter
$css = apply_filters('dynamic_css', '.your_code{}');
$content = $css;
//the rest of the function which generates and upload the file
}
//other functions to enqueue the generated file
}
$compiler = new compiler();
class my_class
{
public function __construct() {
//blablabla
$this->pluginSetup();
}
public function pluginSetup(){
//other functions to save css in option "custom css"
add_action('admin_init', array($this, 'set_css'));
}
public function set_css(){
add_filter( 'dynamic_css', array( $this, 'load_custom_css' ));
}
public function load_custom_css(){
//css has been inserted in option "custom css" through omitted functions
$css = get_option("custom_css");
return $css;
}
}
$my_class = new my_class();
The file is generated and enqueued correctly, but the filter dynamic_css doesn't work and I only get the default value of .your_code{}.
The code in option("custom_css") exists and is valid on var_dump().
I want to keep separated these 2 classes and I cannot find a solution, I tried to change the order of initialisation without success.
I'm building a plugin which generates custom css, I have 2 classes:
- The main class, with core functions
- The compiler class
as following (I simplify everything, because there is a lot of options):
class compiler
{
public function __construct(){
$this->generateCss();
$this->enqueueCss();
//other functions
}
public function generateCss(){
//this is the filter
$css = apply_filters('dynamic_css', '.your_code{}');
$content = $css;
//the rest of the function which generates and upload the file
}
//other functions to enqueue the generated file
}
$compiler = new compiler();
class my_class
{
public function __construct() {
//blablabla
$this->pluginSetup();
}
public function pluginSetup(){
//other functions to save css in option "custom css"
add_action('admin_init', array($this, 'set_css'));
}
public function set_css(){
add_filter( 'dynamic_css', array( $this, 'load_custom_css' ));
}
public function load_custom_css(){
//css has been inserted in option "custom css" through omitted functions
$css = get_option("custom_css");
return $css;
}
}
$my_class = new my_class();
The file is generated and enqueued correctly, but the filter dynamic_css doesn't work and I only get the default value of .your_code{}.
The code in option("custom_css") exists and is valid on var_dump().
I want to keep separated these 2 classes and I cannot find a solution, I tried to change the order of initialisation without success.
Share Improve this question asked Nov 5, 2018 at 20:09 middleladymiddlelady 5531 gold badge6 silver badges18 bronze badges 1 |1 Answer
Reset to default 6The reason you see the default value is that you are adding the filter after the filter has been applied, which results in the default value being used. The filter is applied when the object is constructed, which I'm assuming is before admin_init. If it's after admin_init it won't work either, but that's because the admin_init action has already fired.
Here's a way you could do that (I don't like adding actions/filters in constructors, so the classes are structured a little different.
class compiler
{
public function generateCss(){
$css = apply_filters( 'dynamic_css', '.your_code{}' );
$this->content = $css;
}
}
$compiler = new compiler();
add_action( 'admin_init', [ $compiler, 'generateCss' ], 11 );
class my_class
{
public function set_css(){
add_filter( 'dynamic_css', [ $this, 'load_custom_css' );
}
public function load_custom_css(){
return get_option( 'custom_css' );
}
}
$my_class = new my_class();
add_action( 'admin_init', [ $my_class, 'set_css' ], 10 );
I'm building a plugin which generates custom css, I have 2 classes:
- The main class, with core functions
- The compiler class
as following (I simplify everything, because there is a lot of options):
class compiler
{
public function __construct(){
$this->generateCss();
$this->enqueueCss();
//other functions
}
public function generateCss(){
//this is the filter
$css = apply_filters('dynamic_css', '.your_code{}');
$content = $css;
//the rest of the function which generates and upload the file
}
//other functions to enqueue the generated file
}
$compiler = new compiler();
class my_class
{
public function __construct() {
//blablabla
$this->pluginSetup();
}
public function pluginSetup(){
//other functions to save css in option "custom css"
add_action('admin_init', array($this, 'set_css'));
}
public function set_css(){
add_filter( 'dynamic_css', array( $this, 'load_custom_css' ));
}
public function load_custom_css(){
//css has been inserted in option "custom css" through omitted functions
$css = get_option("custom_css");
return $css;
}
}
$my_class = new my_class();
The file is generated and enqueued correctly, but the filter dynamic_css doesn't work and I only get the default value of .your_code{}.
The code in option("custom_css") exists and is valid on var_dump().
I want to keep separated these 2 classes and I cannot find a solution, I tried to change the order of initialisation without success.
I'm building a plugin which generates custom css, I have 2 classes:
- The main class, with core functions
- The compiler class
as following (I simplify everything, because there is a lot of options):
class compiler
{
public function __construct(){
$this->generateCss();
$this->enqueueCss();
//other functions
}
public function generateCss(){
//this is the filter
$css = apply_filters('dynamic_css', '.your_code{}');
$content = $css;
//the rest of the function which generates and upload the file
}
//other functions to enqueue the generated file
}
$compiler = new compiler();
class my_class
{
public function __construct() {
//blablabla
$this->pluginSetup();
}
public function pluginSetup(){
//other functions to save css in option "custom css"
add_action('admin_init', array($this, 'set_css'));
}
public function set_css(){
add_filter( 'dynamic_css', array( $this, 'load_custom_css' ));
}
public function load_custom_css(){
//css has been inserted in option "custom css" through omitted functions
$css = get_option("custom_css");
return $css;
}
}
$my_class = new my_class();
The file is generated and enqueued correctly, but the filter dynamic_css doesn't work and I only get the default value of .your_code{}.
The code in option("custom_css") exists and is valid on var_dump().
I want to keep separated these 2 classes and I cannot find a solution, I tried to change the order of initialisation without success.
Share Improve this question asked Nov 5, 2018 at 20:09 middleladymiddlelady 5531 gold badge6 silver badges18 bronze badges 1-
apply_filter( 'dynamic_css', ... )is being run before theadd_filter(). – Nathan Johnson Commented Nov 5, 2018 at 20:41
1 Answer
Reset to default 6The reason you see the default value is that you are adding the filter after the filter has been applied, which results in the default value being used. The filter is applied when the object is constructed, which I'm assuming is before admin_init. If it's after admin_init it won't work either, but that's because the admin_init action has already fired.
Here's a way you could do that (I don't like adding actions/filters in constructors, so the classes are structured a little different.
class compiler
{
public function generateCss(){
$css = apply_filters( 'dynamic_css', '.your_code{}' );
$this->content = $css;
}
}
$compiler = new compiler();
add_action( 'admin_init', [ $compiler, 'generateCss' ], 11 );
class my_class
{
public function set_css(){
add_filter( 'dynamic_css', [ $this, 'load_custom_css' );
}
public function load_custom_css(){
return get_option( 'custom_css' );
}
}
$my_class = new my_class();
add_action( 'admin_init', [ $my_class, 'set_css' ], 10 );
本文标签: pluginsUsing addfilter inside another class
版权声明:本文标题:plugins - Using add_filter inside another class 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749203664a2332204.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


apply_filter( 'dynamic_css', ... )is being run before theadd_filter(). – Nathan Johnson Commented Nov 5, 2018 at 20:41