Steamer Lane Studio技術備忘録ワードプレス

WordPress自作テーマのfunction.php-なくてもいいけどあった方がいいコード

wordpress WordPress自作テーマのfunction.php-なくてもいいけどあった方がいいコード
最終更新日: 2023年4月15日

前に必須ものだけ集めてかいたけど、それだけじゃ正直うまくない。
で、もう少しすっきりさせるまたはやりやすくさせるコードをいくつか。

// メインコンテンツの横幅 無くていいけど一応書いておく
if ( ! isset( $content_width ) ) $content_width = 1000;
// フィードのlink要素を自動出力する ブログだから。無くていいか。
add_theme_support( 'automatic-feed-links' );

//the_archive_titleの余計な文字列を消す ないと余計なものが入るからある意味必須。

add_filter( 'get_the_archive_title',
function ($title) {
if (is_category()) {
$title = single_cat_title('',false);
}
elseif (is_tag()) {
$title = single_tag_title('',false);
}
elseif (is_tax()) {
$title = single_term_title('',false);
}
elseif (is_post_type_archive() ){
$title = post_type_archive_title('',false);
}
elseif (is_date()) {
$title = get_the_time('Y年n月');
}
elseif (is_search()) {
$title = '検索結果:'.esc_html( get_search_query(false) );
}
elseif (is_404()) {
$title = '「404」ページが見つかりません';
}
else {

}
return $title;
});

//余計な(ではないが)robotタグ robotタグを自前でコントロールするなら自動で入るものは消す

remove_filter( 'wp_robots', 'wp_robots_max_image_preview_large' );
//投稿ページでCSSを反映 特にクライアント様にはあった方が良いでしょう。
function my_editor_style_setup() {
add_theme_support( 'editor-styles' );

add_editor_style( '../../../../css/common.css' );//ここはCSSの位置を相対で。
}
add_action( 'after_setup_theme', 'my_editor_style_setup' );

//5以降テーマ以外のCSS不使用 CSSをすっきりさせたいならプラグイン等のCSSは反映させない。

function dequeue_plugins_style() {
//プラグインIDを指定し解除する
wp_dequeue_style('wp-block-library');
}

add_action( 'wp_enqueue_scripts', 'dequeue_plugins_style', 9999);

//投稿ページのテーブルのstyle削除 上に関連してテーブルに対する余計なお世話を削除して自分でコントロール。

function tinymce_custom($settings) {

$invalid_style = array(
'table' => 'width height',
'th' => 'width height',
'td' => 'width height'
);

$settings['invalid_styles'] = json_encode($invalid_style);
return $settings;
}

add_filter('tiny_mce_before_init', 'tinymce_custom', 0);
//削除するための関数 head内に勝手に書かれるもので要らないやつ。
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');

// generator meta generator特に要らない

remove_action( 'wp_head','wp_generator');

// canonical ページ毎自分で色々やる

remove_action('wp_head', 'rel_canonical');

// rel="shortlink" 不要なタグ

remove_action( 'wp_head','wp_shortlink_wp_head',10, 0 );
// WLW(Windows Live Writer) wlwmanifest.xml 不要でしょ
remove_action( 'wp_head','wlwmanifest_link');

// RSD xmlrpc.php?rsd 要らない

remove_action( 'wp_head','rsd_link');

remove_action( 'wp_head', 'feed_links', 2 );

remove_action( 'wp_head', 'feed_links_extra', 3 );

function my_delete_local_jquery() {
wp_deregister_script('jquery');
}
add_action( 'wp_enqueue_scripts', 'my_delete_local_jquery' );