集成一个编译器

Integrating a Compiler

/ Core  /  Advanced  / Integrating a Compiler

This article deals specifically with integrating a basic compiler hook for any Redux field. For an in depth article on how to use the compiler hook to dynamically generate a CSS file, please view the Updating a CSS File Dynamically article.

Setting up the Field

Each and every Redux field has a `compiler` argument. By setting this argument to true, a specified hook will fire whenever one of those field’s value changes.

Creating this magic is really quite easy. Let’s begin with this basic field:

1
2
3
4
5
6
7
8
9
array (
     'id'       => 'text' ,
     'type'     => 'text' ,
     'title'    => __( 'Test Compiler' , 'redux-framework-demo' ),
     'subtitle' => __( 'This is to test the compiler hook.' , 'redux-framework-demo' ),
     'desc'     => __( 'Each time this field is set, a flag is set. On save, that flag initiates a compiler hook!' , 'redux-framework-demo' ),
     'compiler' => true,
     'default'  => 'Test Compiler'
),

Note the 'compiler' => true argument. This sets the compiler flag. Now we need to hook into the fired hook. Add this snippet to your code:

Setting up the Compiler Function

Next, the compiler function itself needs to be set up. It requires two parts. The add_filter statement, and the actual function. Ideally, these codes would be placed within your config php file, however, it can be used anywhere in your code provided the opt_name portion of the add_filter line is replaced with the value specified in your opt_name argument. For this example, we’ll be using the example found in the sample-config.php.

In the initSettings section of the sample-config.php, sure the following line is included and/or uncommented:

1
add_filter( 'redux/options/' . $this ->args[ 'opt_name' ] . '/compiler' , array ( $this , 'compiler_action' ), 10, 3);

Now, add (or uncomment) the following function to the Redux_Framework_sample_config class. This is our test function that will allow you see when the compiler hook occurs. It will only fire if a field set with 'compiler' => true is changed.

Please note that for this example, $css will return empty as this is only a basic compiler hook.

1
2
3
4
5
6
7
function compiler_action( $options , $css , $changed_values ) {
     echo '<h1>The compiler hook has run!</h1>' ;
     
     print_r ( $options );
     print_r ( $css );
     print_r ( $changed_values );
}

If all has been set up correctly, you will see the compiler hook message and the passed values on your options panel after the field with the active compiler hook’s value has changed and settings saved.

Please note that if the output_tag argument is set to to false, Redux will not auto-echo a tag into the page header.

Tagged: compiler hook

Related Articles

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值