0

This is my entire code and it is working perfectly in my local system xampp but not working on server.

function taqyeem_dequeue_scrips() {

   if(basename($_SERVER['REQUEST_URI'])=='my_page' || basename($_SERVER['REQUEST_URI'])=='')
    {
            wp_dequeue_style( 'taqyeem-style' );
            wp_dequeue_script( 'taqyeem-main' );
            wp_deregister_script( 'comment-reply' );



    }
}
add_action( 'init', 'taqyeem_dequeue_scrips' );

function dequeue_scrips() {


if(basename($_SERVER['REQUEST_URI'])=='my_page')
    {
            //css

            wp_dequeue_style( 'taxonomy-image-plugin-public' );
            wp_dequeue_style( 'job-alerts-frontend' );
            wp_dequeue_style('validate-engine-css');
            wp_dequeue_style('cp-shortcode');
            wp_dequeue_style('wsl-widget');
            wp_dequeue_style('wp-job-manager-applications-frontend');
            wp_dequeue_style('wp-job-manager-bookmarks-frontend');
            wp_dequeue_style('wp-job-manager-resume-frontend');



            wp_dequeue_style('wp-job-manager-frontend');

            wp_dequeue_style('cp-widgets-css');
            wp_dequeue_style('responsive-css');
            wp_dequeue_style('owl-css');
            wp_dequeue_style('svg-css');
            wp_dequeue_style('cp-burgermenucss');
            wp_dequeue_style('law-bx-slider-css');
            wp_dequeue_style('prettyPhoto');
            wp_dequeue_style('cp-bootstrap');
            wp_dequeue_style('cp-wp-commerce');

            wp_dequeue_style('cp-bx-slider');

            wp_dequeue_style('googleFonts');
            wp_dequeue_style('googleFonts-heading');
            wp_dequeue_style('menu-googleFonts-heading');
            wp_dequeue_style('wppb_stylesheet');
            wp_dequeue_style('A2A_SHARE_SAVE');

            //Script
            wp_dequeue_script( 'html5shiv' );
            wp_dequeue_script( 'cp-bootstrap' );
            wp_dequeue_script( 'addtoany' );
            wp_dequeue_script( 'cp-owl-js' );
            wp_dequeue_script( 'cp-velocity' );
            wp_dequeue_script( 'owl-kenburns' );
            wp_dequeue_script( 'cp-burgermenu' );
            wp_dequeue_script( 'cp-burgermenucustom' );
            wp_dequeue_script( 'cp-bx-slider' );
            wp_dequeue_script( 'cp-custom' );
            wp_dequeue_script( 'prettyPhoto' );
            wp_dequeue_script( 'cp-pscript' );
            wp_dequeue_script( 'cp-scripts_modernizr' );
            wp_dequeue_script( 'cp-scripts' );
            wp_dequeue_script( 'cp-scripts-workmark' );
            wp_dequeue_script( 'cp-easing' );
            wp_dequeue_script( 'cp-bx-slider' );

    }
}
add_action( 'wp_enqueue_scripts', 'dequeue_scrips' );

Any suggestion please. I am working to improve Google Page Speed to get atleast 90 score in mobile and desktop. Currently itis poor in mobile (56 )

0

If you want to dequeue scripts & styles for a specific page based on the slug of the page. You can get the slug in various ways and use it instead of $_SERVER['REQUEST_URI'].

Use if( is_page( array( 'my_page') instead of if(basename($_SERVER['REQUEST_URI'])=='my_page')

function wpse_289574_dequeue_scrips() {
   if(is_page( array( 'my_page')){
       wp_dequeue_style( 'taxonomy-image-plugin-public' );
       ...       
   }
}

How to get the slug of a page:

$qo = $GLOBALS['wp_the_query']->get_queried_object();
$cp = sanitize_post($qo);
$slug = $cp->post_name;

Taken from: How to retrieve the slug of current page?

function wpse_289574_dequeue_scrips() {
   if($slug =='my_page'){
       wp_dequeue_style( 'taxonomy-image-plugin-public' );
       ...       
   }
}

Current hook to dequeue is wp_enqueue_scripts.

  • I tried to echo $slug; but its not working. – vanurag Dec 27 '17 at 11:26
  • I tried is_page() and it is not working in my functions.php file so I used that method. – vanurag Dec 27 '17 at 11:27
  • $slug outputs nothing – vanurag Dec 27 '17 at 11:27
  • Yeah! The script handler are absolutely correct. and few works with init hook and rest with wp_enqueue_scripts – vanurag Dec 27 '17 at 11:30
  • is_page('my_page') not working – vanurag Dec 27 '17 at 11:31
0

I tried below code which is working for me:

add_action('get_header', function() {
    if ( is_page( 'my_page' ) ) {    
   add_action( 'init', 'taqyeem_dequeue_scrips' );
   add_action( 'wp_enqueue_scripts', 'dequeue_scrips' );

    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.