2

I'm building a plugin which generates custom css, I have 2 classes:

  1. The main class, with core functions
  2. 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.

1
  • apply_filter( 'dynamic_css', ... ) is being run before the add_filter(). – Nathan Johnson Nov 5 '18 at 20:41
4

The 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 );
1
  • Awesome Nathan, thanks. I actually included the class into the other class so it would load only when needed but the hook timeline explanation has been resolutive. – middlelady Nov 7 '18 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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