21 posts
  • Has been part of the Envato Community for over 3 years
  • Has collected 100+ items on Envato Market
  • Located in Sweden
beowulf says

Hi everyone! I’m hoping someone can help me out with this.

I’m working on a WordPress theme that gives the user multiple slider options. The sliders load different jquery files which I’d like to wp_enqueue depending on if that particular slider is chosen.

I have an options panel set up where the user can select the slider style they want. My question is how can I wp_enqueue a script conditionally after checking that they chose that option?

I took a stab at it but it doesn’t seem to work:


function slider_content_scripts() {
    $slider_choice == of_get_option('slider_choice');

    if ( (is_front_page()) && ($slider_choice == 'content') ) {

        wp_register_script('slides', get_template_directory_uri() . '/js/slides.min.jquery.js', 'jquery');

        wp_enqueue_script('slides');
    }
}
add_action('wp_print_scripts', 'slider_content_scripts');

First, I’m trying to get the theme slider option the user has chosen with $slider_choice. Then I’m checking if it’s both the front page and the slider choice variable equals ‘content’. If both of those conditions are true then it should spit out the jquery file for the slider.

Can anyone point me in the right direction why this isn’t working?

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

First of all I would echo $slider_choice; after you get the option to see if it is valorized correctly…

Parker

21 posts
  • Has been part of the Envato Community for over 3 years
  • Has collected 100+ items on Envato Market
  • Located in Sweden
beowulf says

Thanks for the response, P&K.

echo $slider_choice; doesn’t print anything in the header. Is it possible that functions.php (where the wp_enqueue function resides) is loading before the theme options which means the variable isn’t populating with that choice?

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

Probably, try enqueuing the scripts on init (no problems with front end scripts):

add_action(‘init’, ‘slider_content_scripts’);

You should check how and when you init your admin.

Parker

204 posts Bold WordPress Theme Development
  • Has been part of the Envato Community for over 5 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Interviewed on an Envato blog
+6 more
srhnbr says

@beowulf Is there a particular reason why you register and then enqueue your script?

“The wp_enqueue_script function does the same thing as wp_register_script except that the src variable is optional. If a src is provided, the enqueue function automatically registers the script for you, thus making the wp_register_script function somewhat unnecessary. “

Also, you should add your hook on “template_redirect”.

Try this:

function slider_content_scripts() {
    $slider_choice = of_get_option('slider_choice');

    if ( is_front_page() && $slider_choice == 'content') {

       wp_enqueue_script('slides', get_template_directory_uri() . '/js/slides.min.jquery.js', 'jquery');
    }
}

add_action('template_redirect', 'slider_content_scripts');
21 posts
  • Has been part of the Envato Community for over 3 years
  • Has collected 100+ items on Envato Market
  • Located in Sweden
beowulf says

Is there a particular reason why you register and then enqueue your script?

Honestly I was just following the WP codex page for wp_enqueue_script but if I can just use wp_enqueue_script without also registering then I’ll do that.

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Can you tell me why it’s better to add a hook on ‘template_redirect’ instead of ‘wp_print_scripts’?

Also, trying your function, srhnbr, still doesn’t work.

EDIT : Hang on! I think I had a typo when I pasted over your function. It’s working now! Holy cow….

204 posts Bold WordPress Theme Development
  • Has been part of the Envato Community for over 5 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Interviewed on an Envato blog
+6 more
srhnbr says

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

Can you tell me why it’s better to add a hook on ‘template_redirect’ instead of ‘wp_print_scripts’?

Well, you should definitely not use ‘wp_print_scripts’ because that same source: http://codex.wordpress.org/Function_Reference/wp_enqueue_script recomends not to.

Note: This function will not work if it is called from a wp_head action, as the tags are output before wp_head runs. Instead, call wp_enqueue_script from an init action function (to load it in all pages), template_redirect (to load it in public pages only), or admin_print_scripts (for admin pages only). Do not use wp_print_scripts (see here for an explanation).

From the above you see that

the wp_print_scripts should be avoided,

the init hook that ParkerAndKent suggested will load your script on all pages, admin and public (back-end and front-end) whereas

the template_redirect will load your script only on the front-end which makes sense from a performance stand-point.

Hope that helps :)

3256 posts
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 more
ParkerAndKent says

^

Well, it’s his business to separate the loading of scripts for admin and front… I still think that the init hook is the best for enqueuing scripts. At the end it’s enough to check is_admin() to load the right scripts for admin and front…

Parker

21 posts
  • Has been part of the Envato Community for over 3 years
  • Has collected 100+ items on Envato Market
  • Located in Sweden
beowulf says

Thank you both for your help! It’s certainly set me on the right track to doing wp_enqueue the right way.

by
by
by
by
by
by