Webデザインやコーディング、WordPressのTipsを発信するブログ

WordPress のテーマの functions.php に入れておくと便利な設定の例です。
セキュリティ強化、管理画面の見た目調整、パフォーマンス改善、開発効率向上など、さまざまな用途をカバーしています。
// アイキャッチ画像(Featured Image)を有効化
add_theme_support( 'post-thumbnails' );
// HTML5 マークアップ対応
add_theme_support( 'html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
) );
// カスタムロゴ対応
add_theme_support( 'custom-logo' );
function mytheme_enqueue_scripts() {
// メインのスタイルシート
wp_enqueue_style( 'theme-style', get_stylesheet_uri(), array(), '1.0' );
// メインの JavaScript
wp_enqueue_script( 'theme-script', get_template_directory_uri() . '/js/main.js', array('jquery'), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_scripts' );
// WordPress のバージョン情報を非表示
remove_action( 'wp_head', 'wp_generator' );
// Windows Live Writer 用リンク削除
remove_action( 'wp_head', 'wlwmanifest_link' );
// RSD(Really Simple Discovery)リンク削除
remove_action( 'wp_head', 'rsd_link' );
// REST API ヘッダー削除(必要に応じて)
remove_action( 'wp_head', 'rest_output_link_wp_head' );
remove_action( 'template_redirect', 'rest_output_link_header', 11 );
function disable_wp_emojicons() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
}
add_action( 'init', 'disable_wp_emojicons' );
function remove_dashboard_widgets() {
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' ); // クイック投稿
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' ); // アクティビティ
remove_meta_box( 'dashboard_primary', 'dashboard', 'side' ); // WordPressニュース
}
add_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );
function custom_excerpt_length( $length ) {
return 50; // 50語に設定
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// 抜粋の末尾を「…」に変更
function custom_excerpt_more( $more ) {
return '…';
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
// XML-RPC を完全に無効化
add_filter( 'xmlrpc_enabled', '__return_false' );
// REST API の認証されたエンドポイント以外を無効化
add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
return $result;
}
if ( ! is_user_logged_in() ) {
return new WP_Error( 'rest_not_logged_in', 'REST API にアクセスするにはログインが必要です。', array( 'status' => 401 ) );
}
return $result;
});
// コアの自動更新を無効化
add_filter( 'automatic_updater_disabled', '__return_true' );
// プラグインだけ自動更新を有効化
add_filter( 'auto_update_plugin', '__return_true' );
// テーマの自動更新を無効化
add_filter( 'auto_update_theme', '__return_false' );
// ログイン画面のロゴリンク先を自サイトに
function custom_login_logo_url() {
return home_url();
}
add_filter( 'login_headerurl', 'custom_login_logo_url' );
// 管理画面のフッター表記変更
function custom_admin_footer() {
echo '<a href="https://w-tips.com" target="_blank">W-Tips.com</a>';
}
add_filter( 'admin_footer_text', 'custom_admin_footer' );
これらを組み合わせることで、テーマの機能拡張やサイトのパフォーマンス・セキュリティ向上、開発効率アップが図れます。
必要に応じてコメントアウトや調整を行い、ご自身の環境に最適化してください。
最後までお読みいただきありがとうございました!