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.
apply_filter( 'dynamic_css', ... )
is being run before theadd_filter()
.