Redux Wordpress Framework enqueue style

547 Views Asked by At

I use the redux framework in my wordpress theme.

My global variable is $ua_options

So I created a switch in my config.php:

array(
   'id'       => 'ua-enable-responsive',
   'type'     => 'switch',
   'title'    => __( 'Responsive', 'redux-framework-demo' ),
   'compiler' => 'true',
   'mode'     => false,
),

In my functions.php I want to enqueue a file conditionally:

if ($ua_options['ua-enable-responsive']) {
    wp_enqueue_style( 'mytheme-bootstrap', get_template_directory_uri() . '/css/bootstrap.css'      );
}
else {
    wp_enqueue_style( 'mytheme-non-responsive', get_template_directory_uri() . '/css/no-responsive.css' );      
}

But that doesn't seem to work. The second file no-responsive.css gets always loaded no matter if I turn the switch to on in the backend.

I also called the global variable at the top of my functions.php

global $ua_options;

Does somebody have an Idea why this wont work? Also is there a way to show such errors / warnings?

4

There are 4 best solutions below

0
On BEST ANSWER

Lead dev of Redux here. You need to call your Redux config BEFORE you load this code. If you can't do that, then you have to do a get_option('opt_name') to get the values.

Basically I bet you'll find that $us_options is empty because it hadn't been created yet. ;)

0
On

Ok I needed to call the global variable inside the scripts enqueue function.

0
On

try this in header.php . also fix this

    if ($ua_options['ua-enable-responsive'] == '1') {
// '1' true and '0' false .
        wp_enqueue_style( 'mytheme-bootstrap', get_template_directory_uri() . '/css/bootstrap.css'      );
    }
    else {
        wp_enqueue_style( 'mytheme-non-responsive', get_template_directory_uri() . '/css/no-responsive.css' );      
    }
0
On

You could try something like this

$redux_options = maybe_unserialize( get_option( 'xyz_redux', false ) );

if( $redux_options['custom-js'] ) {
  add_action('wp_enqueue_scripts', 'load_custom_js');
}

Where xyz_redux is the name of the global variable of the Redux option, it is also the option name in the database.

$redux_options['custom-js'] is the ID of the redux field.

As per the question it would be $redux_options['ua-enable-responsive']

You will need to write a function for enqueuing ike

function load_custom_js() {
  wp_register_script('custom-js', 'path/to/js', array(), '1.0', true );
  wp_enqueue_script('custom_js');
}

I have a question for the lead dev, isn't this a waste of resource? I mean I will need to call this

$redux_options = maybe_unserialize( get_option( 'xyz_redux', false ) );

each time I want something from redux options? (Since the redux option would be empty as per your answer)