0

so I have been trying to get plugin scripts/css to load only when needed. I have a catagory for posts called "review" , below in my functions.php I am trying to load the plugins only on posts with catagory "review", instead , no matter what page/post/home screen..nothign is loading. any help possible?

add_action( 'init', 'stopmyrpjs', 100 );
function stopmyrpjs() {
if (!in_category('review')){
    remove_action('wp_head', 'myrp_load_necessary_js'); 
    remove_action('wp_head', 'myrp_track_traffic'); 
        wp_deregister_script( 'myrp-stuff' );
    wp_dequeue_script( 'jtip' );
    wp_dequeue_script( 'jquery-MetaData' );
    wp_dequeue_script( 'jquery-ui' );
    wp_dequeue_script( 'myrp-stuff' );
    wp_dequeue_script( 'jquery-ui-accordion' );
    wp_dequeue_script( 'jscolor' );
    wp_dequeue_script( 'sorttable' );
    wp_dequeue_script( 'jquery-MetaData' );
    wp_dequeue_script( 'jquery-rating' );
    }}

edit:: I have it working except for the fact that it wont work for is_category or in_category.. what I have gotten to work is below:

add_action( 'wp_print_scripts', 'stopmyrpjs', 100 );
function stopmyrpjs() {
if (!is_single()){
    remove_action('wp_head', 'myrp_load_necessary_js'); 
    remove_action('wp_head', 'myrp_track_traffic'); 
wp_deregister_script( 'myrp-stuff' );
    wp_dequeue_script( 'jtip' );
    wp_dequeue_script( 'jquery-MetaData' );
    wp_dequeue_script( 'jquery-ui' );
    wp_dequeue_script( 'myrp-stuff' );
    wp_dequeue_script( 'jquery-ui-accordion' );
    wp_dequeue_script( 'jscolor' );
    wp_dequeue_script( 'sorttable' );
    wp_dequeue_script( 'jquery-MetaData' );
    wp_dequeue_script( 'jquery-rating' );
    }}

still wish I could have this work for certain categories instead..

1
  • found on the internet that in_category is only used in "the loop", any other options for same functionality but in the functions.php? Commented Sep 17, 2012 at 17:11

2 Answers 2

1

I think you want is_category

This can be used outside the loop, and returns a boolean depending on the current view.

UPDATE:

Looking more closely at your action hook, the reason why "it isn't working" is because of the prioritization of actions. You're trying to deregister and dequeue scripts at the 'init' action, which is probably firing BEFORE the scripts themselves are actually registered and/or enqueued.

I'm also guessing that the external scripts are being registered by various plugins, each with their own action hooks and priorities.

Instead of using the init hook, try changing it to 'wp_footer' to ensure that ALL scripts are loaded by their corresponding plugins before you remove any superfluous scripts:

if (!is_category('review'))
{
    remove_action('wp_head', 'myrp_load_necessary_js'); 
    remove_action('wp_head', 'myrp_track_traffic'); 
    add_action('wp_footer', 'stopmyrpjs');
}
function stopmyrpjs(){
    wp_deregister_script( 'myrp-stuff' );
    wp_dequeue_script( 'jtip' );
    wp_dequeue_script( 'jquery-MetaData' );
    wp_dequeue_script( 'jquery-ui' );
    wp_dequeue_script( 'myrp-stuff' );
    wp_dequeue_script( 'jquery-ui-accordion' );
    wp_dequeue_script( 'jscolor' );
    wp_dequeue_script( 'sorttable' );
    wp_dequeue_script( 'jquery-MetaData' );
    wp_dequeue_script( 'jquery-rating' );
}

Also, bear in mind that wp_dequeue_script is a relatively new function (since version 3.1). If for whatever reason you're still having trouble, try changing wp_dequeue_script to wp_deregister_script.

Some further checks would be to make sure that the function itself is firing, and that the appropriate view causes your conditional to evaluate to true.

Hope this helps, and good luck.

Sign up to request clarification or add additional context in comments.

4 Comments

current view? I read in codex that it is if it is the Catagory Archive page, I have tried is_category('review') already and it has not worked.. should I be add_action to init or wp_print_scripts? Not sure why this is harder then it should be..
I've added some more information for you that might help. I'm thinking this is probably just a prioritization issue with Wordpress. Also, you should probably make sure this isn't a caching issue as well and disable any caching plugins, as well as clear your browser cache. Aside from that, I'm out of ideas.
I have tried your suggestion, didn't seem to work with dequeue or deregister..I have gotten it working using an example I editted in OP, however, it still not doesn't work for is_category or in_category. However, I will accept your answer
Sorry I couldn't be of much more help. If you find a working solution for your problem, feel free to update with your own answer and accept that one.
0

Script Logic is a WordPress plugin that gives you full control over all JavaScript and CSS files. You can conditionally load CSS and JS file only on pages where needed by using WP's conditional tags.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.