워드프레스 플러그인 셋업 후 해당 플러그인이 모든 페이지에서 js, css를 로드할 경우가 있다. 
메인 페이지(homepage), 카테고리 페이지, 아카이브 페이지, 404 페이지, 검색결과 페이지 등 기사 페이지나 커스텀 페이지를 제외하고 플러그인 js, css를 미로딩처리하고 싶은데 플러그인을 이용하지않고 워드프레스 훅을 이용하여 간단히 처리할 수 있다.

 

1. Dequeue the Plugin Script

function wp_remove_scripts() {
	if ( !is_single() ) {
		wp_dequeue_script('front_js');
        wp_deregister_script('front_js');
	}
}
add_action( 'wp_enqueue_scripts', 'wp_remove_scripts', 99 );

 

 

2. Disable Plugin Style I - Disable script and style elsewhere (frontend & backend)

function wp_remove_styles1() {
	if ( !is_single() ) {
		wp_deregister_style('front_css');
	}
}
add_action('wp_print_styles', 'wp_remove_styles1', 100);

 

3. Disable Plugin Style II (푸터 훅 적용) - Dequeue script or style : 잘 안되는 것 같음

function wp_remove_styles2() {
	if ( !is_single() ) {
		wp_dequeue_style('front_css');
	}
}
//add_action('get_footer', 'wp_remove_styles2');


4. 워드프레스 기본 wp_head 액션에 삽입되는 불필요 스크립트 및 요소 제거

Web/ Design/ Vista : Remove Unnecessary Scripts from Header in WordPress
Remove really simple discovery service links : 
remove_action('wp_head', 'rsd_link');

Remove wordpress version number : 
remove_action('wp_head', 'wp_generator'); 

Remove rss feed links : 
remove_action('wp_head', 'feed_links', 2); 

Remove extra feed links : 
remove_action('wp_head', 'feed_links_extra', 3); 

Remove link for windows live writer support : 
remove_action('wp_head', 'wlwmanifest_link'); 

Remove post relational links : 
remove_action('wp_head', 'adjacent_posts_rel_link');   

Remove emoji related scripts & Styles : 
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

Remove shortlink tag : 
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

So putting these code snippets together : 
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator'); 
remove_action('wp_head', 'feed_links', 2); 
remove_action('wp_head', 'feed_links_extra', 3); 
remove_action('wp_head', 'wlwmanifest_link'); 
remove_action('wp_head', 'adjacent_posts_rel_link');     
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');   
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

 

Ref. : 
- Digging Into WordPress > How to Disable CSS and JavaScript Added by Plugins
- WP Alpha > How to remove unused Javascript in WordPress

Posted by cpu21