WordPress

WordPressのヘッダをきれいにする

'15.11.08

WordPressのヘッダ内を整理します。

以下のコードをfunctions.phpに記載。

ヘッダ内の余分なタグを削除

//ヘッダ内の整理
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'wp_shortlink_header');
remove_action('wp_head','wp_print_styles',8);
remove_action('wp_head', 'print_emoji_detection_script', 7 );
remove_action('wp_print_styles', 'print_emoji_styles' );
remove_action('wp_head','rest_output_link_wp_head');
remove_action('wp_head','wp_oembed_add_discovery_links');
remove_action('wp_head','wp_oembed_add_host_js');

function remove_dns_prefetch( $hints, $relation_type ) {
    if ( 'dns-prefetch' === $relation_type ) {
        return array_diff( wp_dependencies_unique_hosts(), $hints );
    }
    return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );

// text/javascript除去
function remove_script_type($tag) {
return str_replace("type='text/javascript' ", "", $tag);
}
add_filter('script_loader_tag','remove_script_type');

jquery.jsとjquery-migrate.min.jsをフッタに移動

//ヘッダ内のJSをフッタに移動
if (!is_admin()) {
function deregister_script(){
wp_deregister_script('jquery');
}
function register_script(){
wp_register_script('jquery', '/wp-includes/js/jquery/jquery.js', array(), '1.11.1', true );
wp_register_script('jquery-migrate', "/wp-includes/js/jquery/jquery-migrate.min.js", array(), '1.2.1', true);
}
function add_script() {
deregister_script();
register_script();
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-migrate');
}
add_action('wp_enqueue_scripts', 'add_script');
}

(View598)