WordPressおすすめ初期設定

WordPress

[スポンサーリンク]


functions.phpは、WordPressのテーマを構成するテンプレートファイルです。

テーマに機能を追加する時に、その内容をfunctions.phpファイルに書きます。

書いてあると便利なコードを紹介します。

/*-------------------------------------------*/
/*  「wp_head();」に出力される余分なタグを削除する
/*—————————————————————*/
  
// WordPressのバージョンを非表示にする
remove_action('wp_head', 'wp_generator');
  
// 前の文書と次の文書へのリンクを非表示にする
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
  
// リモート投稿をする時に使うタグを非表示にする
remove_action('wp_head', 'rsd_link');
  
// リモート投稿をする時に使うタグを非表示にする
remove_action('wp_head', 'wlwmanifest_link');
  
// WordPressの投稿IDを使った短いURLを非表示にする
remove_action('wp_head', 'wp_shortlink_wp_head');
  
//簡単に引用表示に使うを非表示にするタグをを非表示にする
remove_action('wp_head','rest_output_link_wp_head');
  
//簡単に引用表示に使うを非表示にするタグをを非表示にする
  
remove_action('wp_head','wp_oembed_add_discovery_links');
  
//絵文字用のコードを非表示にする
  
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles' );
remove_action('admin_print_styles', 'print_emoji_styles');
 
//RSSフィードのURLを非表示にする
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
 
 
/*-------------------------------------------*/
/*  バージョンアップ通知を管理者のみ表示させるようにします
/*-------------------------------------------*/
 
function update_nag_admin_only() {
    if ( ! current_user_can( 'administrator' ) ) {
        remove_action( 'admin_notices', 'update_nag', 3 );
    }
}
add_action( 'admin_init', 'update_nag_admin_only' );
 
/*-------------------------------------------*/
/*  DNSプリフェッチ用コードを削除 
/*-------------------------------------------*/
 
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );
function remove_dns_prefetch( $hints, $relation_type ) {
    if ( 'dns-prefetch' === $relation_type ) {
        return array_diff( wp_dependencies_unique_hosts(), $hints );
    }
    return $hints;
}
 
/*-------------------------------------------*/
/* /画像の添付ファイルページのリダイレクト
/*-------------------------------------------*/
 
add_action( 'template_redirect', 'attachment404' );
 
function attachment404() {
    // attachmentページだった場合
    if ( is_attachment() ) {
        global $wp_query;
        $wp_query->set_404();
        status_header(404);
    }
}
 
/*-------------------------------------------*/
/*  「?ver=~」を削除する
/*-------------------------------------------*/
function vc_remove_wp_ver_css_js( $src ) {
    if ( strpos( $src, 'ver=' ) )
    $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'vc_remove_wp_ver_css_js', 9999 );
add_filter( 'script_loader_src', 'vc_remove_wp_ver_css_js', 9999 );
 
 
/*-------------------------------------------*/
/*  お問い合わせページ以外にContact Form 7のスクリプトを読み込まなくする
/*-------------------------------------------*/
 
function my_contact_enqueue_scripts(){
wp_deregister_script('contact-form-7');
wp_deregister_style('contact-form-7');
if (is_page('contact')) {
  if (function_exists( 'wpcf7_enqueue_scripts')) {
    wpcf7_enqueue_scripts();
  }
  if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
    wpcf7_enqueue_styles();
  }
}
}
add_action( 'wp_enqueue_scripts', 'my_contact_enqueue_scripts');
 
/*-------------------------------------------*/
/*[Jetpack]使用時に自動で読み込まれるスタイルシートを削除する方法。
/*-------------------------------------------*/
add_filter('jetpack_implode_frontend_css','__return_false' );
 
 
 
/*-------------------------------------------*/
/*  END
/*-------------------------------------------*/


こちらの記事も読まれてます。