WordPress: removing Customizer Custom CSS

Quick tip for WordPress developers: how to remove custom CSS personalizado created by the Customizer. Pretty simple, actually.

[para a versão em português, clique aqui]

Let's say we want page with ID 10 to not use the custom CSS, you just need to use the wp_head hook and remove wp_custom_css_cb function from wp_head actions queue. You would use something like this in your theme functions.php or in site specific plugin:

// first check if we are not in Admin area
if( !is_admin ){

    // get post global variable
    global $post;

    // check if post_id is equals to the post/page/custom_post ID we want to filter the CSS
    if( $post->post_id === 10){

        //remove wp_custom_css_cb from the actions queue on wp_head using the same priority, 101
        remove_action('wp_head', 'wp_custom_css_cb', 101);
    }
}

Obviously, you can use other conditionals for removing the cusotm CSS. You could remove them from archives.

// first check if we are not in Admin area
if( !is_admin ){

    // check if we are in an archive
    if( is_archive() ){

        //remove wp_custom_css_cb from the actions queue on wp_head using the same priority, 101
        remove_action('wp_head', 'wp_custom_css_cb', 101);
    }
}

Quick and simple. If you need, the gists are available on Github.