最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - Using add_filter inside another class

matteradmin8PV0评论

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.

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.

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 the add_filter(). – Nathan Johnson Commented Nov 5, 2018 at 20:41
Add a comment  | 

1 Answer 1

Reset to default 6

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 );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far