themeum / kirki

Extending the customizer
https://kirki.org
MIT License
1.26k stars 328 forks source link

Is there a way to enqueue/dequeue scripts/styles based on option value? #293

Closed BoweFrankema closed 9 years ago

BoweFrankema commented 9 years ago

I'm trying to add an option that allows a user to quickly load a theme preset. In order to do this I am checking an option value and then dequeueing/enqueueing a stylesheet. This functionality works as expected but only when the option value is saved and the customizer is reloaded. This means the preset is not applied "live". Is there any way I could make this work? Here's my current code:

if ( Kirki::get_option('wf_preset') == 'green' ) {

    function wefoster_plus_dequeue_styles() {
        wp_dequeue_style( 'wff_main' );
    }
    add_action( 'wp_enqueue_scripts', 'wefoster_plus_dequeue_styles', 10000 );

    function wefoster_plus_load_preset() {
        wp_enqueue_style('wff_child', plugin_dir_url( __FILE__ ) . '../assets/css/' . Kirki::get_option('wf_preset') . '.css', false);
    }
    add_action('wp_enqueue_scripts', 'wefoster_plus_load_preset', 100);

}
BoweFrankema commented 9 years ago

This code works actually perfectly fine.. I think there was an error in my customizer controls that prevented this from working. This issue can be closed! Hopefully the above snippet will be useful for someone who wants to do the same though :+1:

aristath commented 9 years ago

@BoweFrankema sorry for the delay here... I've been kinda busy and I'm going through all the issues now. I'm glad you managed to figure this one out buddy, I'm closing the issue. :+1:

Cheers!