0

I want to remove css and js file from a template in WordPress by using wp_dequeue_script and wp_dequeue_style function but my code is not working.

Js File: <script type='text/javascript' src='http://localhost/worpress/wp-content/themes/goliath/theme/assets/js/vendor/jquery.hoverintent.min.js'></script>

Css File: <link rel='stylesheet' id='plsh-bbpress-css'  href='http://www.smeadvisor.com/wp-content/themes/goliath/theme/assets/css/bbpress.css' type='text/css' media='all' />

I am using goliath theme and I have added below code in goliath-child theme functions.php file

add_action('wp_print_scripts','example_dequeue_myscript');
function example_dequeue_myscript() {
   wp_dequeue_script( 'jquery.hoverintent' );
}

add_action('wp_print_styles','example_dequeue_mystyle');
function example_dequeue_mystyle() {
   wp_dequeue_style( 'bbpress' );
}

Please help me in these!

4 Answers 4

3

wp_dequeue_script() and wp_dequeue_style() works only if the related CSS and JS files are included in the website by wp_enqueue_script()..

wp_dequeue_script and wp_dequeue_style work by taking the handle as the parameter.

You should have registered as like follows :

wp_register_style('pagination-style', plugins_url('style.css', __FILE__));
wp_enqueue_style('pagination-style');

and script like this :

wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
wp_enqueue_script('jquery'); 

And then use handle for removing css and js as follows:

wp_dequeue_style('pagination-style');
wp_dequeue_script('jquery');

Hope Remove css and js

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

Comments

0

Try

add_action('wp_enqueue_scripts','example_dequeue_myscript', 30);
function example_dequeue_myscript() 
{
    wp_dequeue_script('jquery.hoverintent');
    wp_dequeue_style('bbpress');
}

3 Comments

I think this function is not executing on that page
@okconfused try setting higher priority. 30 should do
not working yet Could you please check the parameter in wp_dequeue_script function is correct or not?
0

You have to enqueue these file in header or footer, not directly write in script or link tag that is proper method and then enqueue your script in condition. like this

   add_action('wp_enqueue_scripts','example_dequeue_mystyle');
   function example_dequeue_mystyle() {
       if(!is_page('my-page-slug')){
            wp_enqueue_style( 'bbpress' );
            wp_enqueue_script( 'jquery.hoverintent' );
       }
   }

Hope you got that.

Comments

0

If the files are enqueued (not hardcoded in title) then the problem may be in wrong script/style names. Try to use this in functios.php first:

function remove_head_scripts() { 
   global $wp_styles;
   var_dump($wp_styles);
   global $wp_scripts;
   var_dump($wp_scripts);
} 
add_action( 'wp_enqueue_scripts', 'remove_head_scripts', 101 );

Then you can see all of the enqueued styles & scripts with their respective slugs printed somewhere inside your html (try to use ctrl+U in browser). Looking in that lists you can locate your desired script & style & find out it's slugs. If you can't see them, try to play with priority (101) or hook (wp_enqueue_scripts)

After you know exact slugs of your script & style, replace snippet above with new one:

function remove_head_scripts() { 
   wp_dequeue_style( 'exact_style_slug' );
   wp_dequeue_script( 'exact_script_slug' );
} 
add_action( 'wp_enqueue_scripts', 'remove_head_scripts', 101 );

Again, you can play with priority & hook. It highly depends on where your theme/plugin is enqueueing that script & style.

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.