owned mediaウェブ制作に役立つコンテンツを発信中!

WordPressのfunctions.phpに記述しておくと便利なコードまとめ

最終更新日: Update!!
WordPressでサイトをカスタマイズする際に必須となるfunctions.phpファイルですが、今回はこのfunctions.phpファイルに記述するコードをまとめてみました。あんなことやこんなことも実装できてしまう便利機能ばかりです!  
タイトルタグ出力
function output_title() {
 add_theme_support('title-tag');
}
add_action('after_setup_theme', 'output_title');
   
タイトルタグの区切り文字変更
function rewrite_separator($separator) {
  $separator = '|';
  return $separator;
}
add_filter('document_title_separator', 'rewrite_separator');
上記の方法でタイトルタグを自動生成した場合、区切り文字はハイフンとなりますが、この処理を加えることで、区切り文字を任意のものに変更することができます。  
アイキャッチ画像の有効化とオリジナルのサムネイルサイズの定義
add_theme_support('post-thumbnails');
add_image_size( 'custom_thumbnails', 80, 80, true );
add_image_sizeには第一引数に定義したサムネイルサイズの名前を、第二引数にはサムネイルのWidth、第三引数にはサムネイルのHeight、第四引数にはトリミングするかをBoolean値で指定します。  
カスタムメニューの有効化
add_theme_support( 'menus' );
   
カスタムロゴの有効化
function custom_logo() {
 add_theme_support('custom-logo');
}
add_action('after_setup_theme', 'custom_logo');
   
RSS用リンク出力
add_theme_support('automatic-feed-links');
   
WordPressバージョン出力metaタグ非表示
remove_action('wp_head','wp_generator');
   
外部ツールの編集用URLの非表示
remove_action('wp_head', 'rsd_link');
   
Windows Live Writerの編集用URLの非表示
remove_action('wp_head', 'wlwmanifest_link');
   
絵文字機能の削除
function disable_emoji() {
 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' ); 
 remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
 remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); 
 remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
}
add_action( 'init', 'disable_emoji' );
   
抜粋の文字数を指定
function custom_excerpt_length($length) {   
  return 140; 
}   
add_filter('excerpt_length', 'custom_excerpt_length');
   
抜粋の文末文字を指定
function custom_excerpt_more($more) {
  return ' ... ';
}
add_filter('excerpt_more', 'custom_excerpt_more');
   
adminbarの非表示
function disable_admin_bar(){
  return false;
}
add_filter( 'show_admin_bar' , 'disable_admin_bar');
   
テーマ内のjQuery読み込み(管理画面への読み込みも防ぐ)
function load_cdn() {
  if ( !is_admin() ) {
  wp_deregister_script('jquery');
  wp_enqueue_script('jquery', './js/jquery-*.**.*.min.js', array(), '*.**.*');
  }
}
add_action('init', 'load_cdn');
   
javascriptファイル読み込み
function register_script() {
  if ( !is_admin() ) {
    wp_deregister_script('jquery');
    wp_enqueue_script('jquery','//code.jquery.com/jquery-*.*.*.min.js', array(), null, 'all');
    wp_enqueue_script('ハンドル名','ファイルパス', array(), null, 'all');
}
add_action('wp_enqueue_scripts', 'register_script');
   
スタイルシート読み込み
function register_stylesheet() {
  wp_enqueue_style('style', get_template_directory_uri().'/style.css', array(), null, 'all');
  wp_enqueue_style('ハンドル名', 'ファイルパス', array(), null, 'all');
}
add_action('wp_enqueue_scripts', 'register_stylesheet');
   
ログイン後にトップページへリダイレクトさせる
function redirect_login_front_page() {
  $url = site_url('', 'http');
  wp_safe_redirect($url);
  exit();
}
add_action('wp_login', 'redirect_login_front_page');
   
ログアウト後にトップページへリダイレクトさせる
function redirect_logout_page(){
  $url = site_url('', 'http');  
  wp_safe_redirect($url);
  exit();
  }
add_action('wp_logout','redirect_logout_page');
   
ウィジェットの追加
function add_widgets_init() {
  register_sidebar(
    array(
      'name' => 'ウィジェット',
      'id' => 'widgets',
      'description' => '管理画面から編集可能なウィジェットです',
      'before_widget' => '<div>',
      'after_widget' => '</div>',
      'before_title' => '',
      'after_title' => ''
    )
  );
}
add_action('widgets_init', 'add_widgets_init');
   
カスタムメニュー定義
function custom_menu() {
  register_nav_menus( array(
    'global' => 'グローバルナビ',
    'header' => 'ヘッダーナビ',
    'footer' => 'フッターナビ'
  ));
}
add_action('after_setup_theme','custom_menu');
   
カスタム投稿タイプ作成
function create_custom_post_type() {
  $labels = array(
    "name" => __( "カスタム投稿名" ),
    "singular_name" => __( "カスタム投稿名"),
  );
  $args = array(
    "label" => __( "カスタム投稿名"),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "delete_with_user" => false,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "カスタム投稿スラッグ", "with_front" => true ),
    "query_var" => true,
    "menu_position" => 5,
    "supports" => array( "title", "editor", "thumbnail", "comments" ),
  );
  register_post_type( "カスタム投稿スラッグ", $args );
}
add_action( 'init', 'create_custom_post_type' );
   
カスタム分類作成
function create_taxonomy() {
  $labels = array(
    "name" => __( "カスタム分類名"),
    "singular_name" => __( "カスタム分類名"),
  );
  $args = array(
    "label" => __( "カスタム分類名"),
    "labels" => $labels,
    "public" => true,
    "publicly_queryable" => true,
    "hierarchical" => true,
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'カスタム分類スラッグ', 'with_front' => true, ),
    "show_admin_column" => false,
    "show_in_rest" => true,
    "rest_base" => "カスタム分類スラッグ",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => false,
    );
  register_taxonomy( "カスタム分類スラッグ", array( "投稿タイプスラッグ" ), $args );
}
add_action( 'init', 'create_taxonomy' );
   
カスタム投稿タイプの月別アーカイブ出力
global $my_archives_post_type;
add_filter( 'getarchives_where', 'my_getarchives_where', 10, 2 );
function my_getarchives_where( $where, $r ) {
  global $my_archives_post_type;
  if ( isset($r['post_type']) ) {
    $my_archives_post_type = $r['post_type'];
    $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
  } else {
    $my_archives_post_type = '';
  }
  return $where;
}
add_filter( 'get_archives_link', 'my_get_archives_link' );
function my_get_archives_link( $link_html ) {
  global $my_archives_post_type;
  if ( '' != $my_archives_post_type )
  $add_link .= '?post_type=' . $my_archives_post_type;
  $link_html = preg_replace("/href=\'(.+)\'\s/","href='$1".$add_link." '",$link_html);
  return $link_html;
}
  このようにfunction.phpはWordPressにいろいろな機能を追加するプラグインのような役割を果たすテンプレートです。このほかにもたくさんのコードが存在しますが、また次の機会にまとめていきたいと思います。
  • はてなブックマーク
  • Pocket
  • Linkedin
  • Feedly

この記事を書いた人

Twitter

sponserd

    keyword search

    recent posts

    • Twitter
    • Github
    contact usscroll to top
      • Facebook
      • Twitter
      • Github
      • Instagram