1

I am trying to dequeue some scripts and styles only for specific custom post type. But when I add this snippet of code the actions are applied for the entire website as the styles and the scripts are dequeued for all other post types and pages where I need them. Thanks in advance!

add_action( 'wp_enqueue_scripts', 'dequeue_my_scripts', 999 );
function dequeue_my_scripts()
{
  if ( 'guide' == get_post_type() )
     wp_dequeue_script( 'dw_focus' );
     wp_dequeue_script( 'comment-reply' );
     wp_dequeue_style( 'style' );
     wp_dequeue_style( 'responsive' );
}
  • try debugging with var_dump(get_post_type()); – David Lee Sep 26 '17 at 21:02
  • added var_dump(get_post_type( 'guide' )); below the { and bool(false) appeared on the very top of every of website's pages – hml Sep 26 '17 at 21:15
1

You are missing the {...} around the calls to wp_dequeue_script. That means only the first one is associated with the if statement, and the rest are being executed for every case.

Update your code to the following:

add_action( 'wp_enqueue_scripts', 'dequeue_my_scripts', 999 );
function dequeue_my_scripts()
{
    if ( 'guide' == get_post_type() )
    { 
        wp_dequeue_script( 'dw_focus' );
        wp_dequeue_script( 'comment-reply' );
        wp_dequeue_style( 'style' );
        wp_dequeue_style( 'responsive' );
    } 
}

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.