ワードプレスのファンクション設定(一覧)
/*-------------------------------------------*//* バージョンアップ通知を管理者のみ表示させるようにします/*-------------------------------------------*/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' );/*-------------------------------------------*//* 絵文字表示用のスクリプトとスタイルの記述を消去/*-------------------------------------------*/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' );/*-------------------------------------------*//* ショートコードを使ったphpファイルの呼び出し方法/* (使用テーマ内のincファイルに格納している場合)/*-------------------------------------------*/function Include_my_php($params = array()) { extract(shortcode_atts(array( 'file' => 'default' ), $params)); ob_start(); include(get_theme_root() . '/' . get_template() . "/inc/$file.php"); return ob_get_clean();}add_shortcode('myphp', 'Include_my_php');// 呼び出し方 [myphp file='test']/*-------------------------------------------*//* 404時にトップに移動/*-------------------------------------------*/function is404_redirect_home() { if( is_404() ){ wp_safe_redirect( home_url( '/' ) ); exit(); }}add_action( 'template_redirect', 'is404_redirect_home' );/*-------------------------------------------*//* URLの入力で埋め込み式表示を停止させる/*-------------------------------------------*/remove_action('wp_head','rest_output_link_wp_head'); // API リンクタグを非表示remove_action('wp_head','wp_oembed_add_discovery_links'); // 埋め込み用のディスカバリリンクの非表示remove_action('wp_head','wp_oembed_add_host_js'); // 埋め込み用の JavaScript の非表示/*-------------------------------------------*//* GoogleキャプチャVer3のアイコンをcontactページのみで表示させる/*-------------------------------------------*/add_action( 'wp_enqueue_scripts', function() { if(is_page('contact')) return; //ここにcontactページのスラッグを入れる wp_deregister_script( 'google-recaptcha' );});/*-------------------------------------------*//* ウィジェットでショートコードを使えるようにする/*-------------------------------------------*/add_filter('widget_text', 'do_shortcode' );function add_category_slug_classes_to_body_classes($classes){ // 投稿ページで最上位のカテゴリスラッグをbodyクラスに追加する global $post; //if ( is_single() ) { $cat = get_the_category(); $cat_now = $cat[0]; $cat_id = $cat_now->cat_ID; $cat_parent = get_category($cat_id); for($i = 1; $i <= 100; $i++){ if($cat_parent -> category_parent == 0){ break; } $cat_parent = $cat_parent -> category_parent; $cat_parent = get_category($cat_parent); } $classes[] = 'category-'.$cat_parent->slug; //} return $classes;}add_filter('body_class', 'add_category_slug_classes_to_body_classes');/*-------------------------------------------*//* コンタクトフォーム7のメアド再入力/*-------------------------------------------*/function wpcf7_main_validation_filter( $result, $tag ) { $type = $tag['type']; $name = $tag['name']; $_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) ); if ( 'email' == $type || 'email*' == $type ) { if (preg_match('/(.*)_confirm$/', $name, $matches)){ $target_name = $matches[1]; if ($_POST[$name] != $_POST[$target_name]) { if (method_exists($result, 'invalidate')) { $result->invalidate( $tag,"確認用のメールアドレスが一致していません"); } else { $result['valid'] = false; $result['reason'][$name] = '確認用のメールアドレスが一致していません'; } } } } return $result;}add_filter( 'wpcf7_validate_email', 'wpcf7_main_validation_filter', 11, 2 );add_filter( 'wpcf7_validate_email*', 'wpcf7_main_validation_filter', 11, 2 );// コンタクトフォームのメアド再入力↓で、CM7の方には[email* your-email_confirm placeholder "確認のためもう一度入力してください"]と入力する。/*-------------------------------------------*//* HEADに記載するもの/*-------------------------------------------*/<?php// head内に挿入したいタグ処理function add_wp_meta_tag() { ?> ↑PHPの終了タグ以降から ここの部分は全てHTMLとして処理されます。 改行もお好きにどうぞ。 何行でも。 ↓このPHPの開始タグの間までお好きに書けます。<?php}// add_actionにてwp_headの動作にadd_wp_meta_tagの処理を追加add_action('wp_head', 'add_wp_meta_tag');?>/*-------------------------------------------*//* HEADに記載するもの/*-------------------------------------------*//*-------------------------------------------*//* CSS、JSなどの読み込み/*-------------------------------------------*/function add_files() { wp_enqueue_style( 'bootstrap-css', get_template_directory_uri().'/css/bootstrap.min.css' ); wp_enqueue_style( 'fontawesome', 'https://use.fontawesome.com/releases/v5.0.13/css/all.css' ); wp_enqueue_style( 'style', get_stylesheet_uri() ); wp_enqueue_script( jQuery, get_template_directory_uri().'/js/jquery-3.3.1.js' ); wp_enqueue_script( popper, get_template_directory_uri().'/js/popper.min.js', '', '', true ); wp_enqueue_script( bootstrap-js, get_template_directory_uri().'/js/bootstrap.min.js', '', '', true ); wp_enqueue_script( jQuery, '/wp-content/themes/norio/js/adobefont.js' ); wp_enqueue_script( jQuery, '/wp-content/themes/norio/js/menu-btn.js' );}add_action( 'wp_enqueue_scripts', 'add_files' );/*-------------------------------------------*//* CSS、JSなどの読み込み/*-------------------------------------------*/function my_scripts() { if ( !is_admin() ) { // 管理画面以外で適用する // WP同封のjQueryを読み込ませない //wp_deregister_script('jquery'); // CDNのjQueryを読み込む //wp_enqueue_script('jquery','https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js',array(),null, true); // テーマフォルダ→jsフォルダ→config.jsをフッターに読み込む(クエリパラメーター自動更新付) wp_enqueue_script('ofi.min',esc_url(get_theme_file_uri('/js/ofi.min.js')), array('jquery'), date('YmdHis'), false ); wp_enqueue_script('adobefont',esc_url(get_theme_file_uri('/js/adobefont.js')), array('jquery'), date('YmdHis'), false); wp_enqueue_script('menu-btn',esc_url(get_theme_file_uri('/js/menu-btn.js')), array('jquery'), date('YmdHis'), false); wp_enqueue_script('velocity.min',esc_url(get_theme_file_uri('/js/velocity.min.js')), array('jquery'), date('YmdHis'), false); wp_enqueue_script('velocity.ui.min',esc_url(get_theme_file_uri('/js/velocity.ui.min.js')), array('jquery'), date('YmdHis'), false); //wp_enqueue_script('velocity',esc_url(get_theme_file_uri('/js/velocity.js')), array('jquery'), date('YmdHis'), false); wp_enqueue_script('smooth-scroll',esc_url(get_theme_file_uri('/js/smoothScroll.js')), array('jquery'), date('YmdHis'), true); wp_enqueue_script('velocity-anime',esc_url(get_theme_file_uri('/js/velocity-anime.js')), array('jquery'), date('YmdHis'), true); //wp_enqueue_script('ani-set',esc_url(get_theme_file_uri('/js/ani-set.js')), array('jquery'), date('YmdHis'), true); //wp_enqueue_script('jquery.min.1.10.2','https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', array('jquery'), false); //wp_enqueue_script('smooth-scroll-basic',esc_url(get_theme_file_uri('/js/smooth-scroll-basic.js')), array('jquery'), true); }}add_action( 'wp_enqueue_scripts', 'my_scripts', 50 );/*-------------------------------------------*//* 前後の記事のリンクURLを取得する/*-------------------------------------------*/function twpp_get_adjacent_post_url( $previous = true ) { $post = get_adjacent_post( false, '', $previous ); $url = ''; if( !empty( $post ) ) { $url = get_permalink( $post->ID ); } return $url;}んでしたのものをPHPにはりつける$prev_url = twpp_get_adjacent_post_url( true );$next_url = twpp_get_adjacent_post_url( false );/*-------------------------------------------*//* bodyのclassにスラッグ名を挿入/*-------------------------------------------*/add_filter( 'body_class', 'add_page_slug_class_name' );function add_page_slug_class_name( $classes ) { if ( is_page() ) { $page = get_post( get_the_ID() ); $classes[] = $page->post_name; } return $classes;}/*-------------------------------------------*//* bodyのclassに直上/親のスラッグ名を挿入/*-------------------------------------------*/add_filter( 'body_class', 'add_page_slug_class_name' );function add_page_slug_class_name( $classes ) { if ( is_page() ) { $page = get_post( get_the_ID() ); $classes[] = $page->post_name; $parent_id = $page->post_parent; if ( $parent_id ) { $classes[] = get_post($parent_id)->post_name . '-child'; } } return $classes;}/*-------------------------------------------*//* bodyのclassに最上スラッグ名を挿入/*-------------------------------------------*/add_filter( 'body_class', 'add_page_slug_class_name' );function add_page_slug_class_name( $classes ) { if ( is_page() ) { $page = get_post( get_the_ID() ); $classes[] = $page->post_name; $parent_id = $page->post_parent; if ( 0 == $parent_id ) { $classes[] = get_post($parent_id)->post_name; } else { $progenitor_id = array_pop( get_ancestors( $page->ID, 'page', 'post_type' ) ); $classes[] = get_post($progenitor_id)->post_name . '-child'; } } return $classes;}/*-------------------------------------------*//* プラグイン「Events Manager」のイベントカテゴリ別にbodyにclassを付ける/* ※カテゴリ名は1こしかつかない/*-------------------------------------------*/add_filter( 'body_class', 'add_event_category_to_body_class');function add_event_category_to_body_class( $classes ) { if ( em_is_event_page() ) { global $post; $EM_Event = em_get_event($post->ID, 'post_id'); $classes[] = $EM_Event->output('#_CATEGORYSLUG'); return $classes; }}/*-------------------------------------------*//* ショートコードを使ったphpファイルの呼び出し方法/* (使用テーマ内のincファイルに格納している場合)/*-------------------------------------------*/function Include_my_php($params = array()) { extract(shortcode_atts(array( 'file' => 'default' ), $params)); ob_start(); include(get_theme_root() . '/' . get_template() . "/inc/$file.php"); return ob_get_clean();}add_shortcode('myphp', 'Include_my_php');// 呼び出し方 [myphp file='test']/*-------------------------------------------*//* the_archive_title 余計な文字を編集/*-------------------------------------------*/add_filter( 'get_the_archive_title', function ($title) { if (is_category()) { $title = single_cat_title('Category/',false); } elseif (is_tag()) { $title = single_tag_title('Tag/',false); } elseif (is_tax()) { $title = single_term_title('Tax/',false); } elseif (is_post_type_archive() ){ $title = post_type_archive_title('',false); } elseif (is_date()) { $title = get_the_time('Y.m'); } elseif (is_search()) { $title = '検索結果:'.esc_html( get_search_query(false) ); } elseif (is_404()) { $title = '「404」ページが見つかりません'; } else { } retu/*-------------------------------------------*//* カスタム投稿一覧に表示項目を増やす/*-------------------------------------------*/function manage_posts_columns($columns) {$col['cb'] = '<input type="checkbox" />';//選択のチェックを残す$col['title'] = "店名";//タイトル部分$col['stores_area'] = "地域";//表示項目をスラッグと表示名で$col['date'] = "更新日";//表示項目をスラッグと表示名で//$col['baby_sale'] = "状態";//$columns['baby_category'] = "種類";//$col['author'] = "作成者";//タイトル部分//$col['categories'] = "カテゴリー";//タイトル部分//$col['tags'] = "タグ";//タイトル部分return $col;}function add_column($column_name, $post_id) {if( $column_name == 'stores_area' ) {//表示させるスラッグを記入$stitle = get_post_meta($post_id, 'stores_area', true);//表示させるスラッグを記入}// if( $column_name == 'baby_sale' ) {//表示させるスラッグを記入//$stitle = get_post_meta($post_id, 'baby_sale', true);//表示させるスラッグを記入//}//if( $column_name == 'baby_category' ) {//表示させるスラッグを記入//$stitle = get_the_term_list($post_id, 'baby_category');//表示させるスラッグを記入//}if ( isset($stitle) && $stitle ) {echo attribute_escape($stitle);} else {echo __('None');}}add_filter( 'manage_edit-stores_columns', 'manage_posts_columns' );//カスタムポストスラッグを記入add_action( 'manage_stores_posts_custom_column', 'add_column', 10, 2 );//カスタムポストスラッグを記入