16
20

More than 1 year has passed since last update.

WordPress functions.php初期設定

Last updated at Posted at 2017-11-19

htmlデータをwordpress化する際の初期functions.phpを纏めました。

##wp本体・プラグイン更新通知を非表示する

functions.php
//WPアップデート通知を非表示
add_filter('pre_site_transient_update_core', create_function('$a', "return null;"));

//プラグイン更新通知を非表示
remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );

// 更新のお知らせを消すためのもの
add_action('admin_menu', 'remove_counts');
function remove_counts(){
	global $menu,$submenu;
	$menu[65][0] = 'プラグイン';
	$submenu['index.php'][10][0] = 'Updates';
}

##アイキャッチ画像
数は増やせます。

functions.php
add_theme_support( 'post-thumbnails' );
add_image_size( 'top_slider', 810, 360, true );//topスライダー
add_image_size( 'w700', 700, 0, true );//横幅700
add_image_size( 'w180', 1800, 0, true );//横幅180

##管理画面の「投稿一覧」と「固定ページ一覧」の最大表示数を変更する

functions.php
function my_edit_posts_per_page ($posts_per_page) {
	return 75;
}
add_filter('edit_posts_per_page', 'my_edit_posts_per_page');

##URLの取得 「/」区切りで配列

functions.php
function my_url(){
	$str = str_replace("/wp/", "/", $_SERVER["REQUEST_URI"]);
	$my_url['url'] = $str;
	$my_url['url'] = substr_replace($my_url['url'], "", 0,1);//一文字目の/を削除
	$my_url['path'] = explode("/", $my_url['url']);
	$my_url['url'] = "/".$my_url['url'];//一応/をいれておく。
	return $my_url;
}

##NEWマーク

functions.php
function add_new($date,$days=7){
	$new_date = date("Y-m-d", strtotime("-".$days." day"));
    if( $date > $new_date ){
        $re = '<span>New!</span>';
    }else{
    	$re = NULL;
    }
    return $re;
}

##文字数制限

functions.php
function na_trim_words($str,$int,$end='…'){
	$post_content = strip_tags($str);
	if(mb_strlen($post_content)>$int ) {
		$post_content = mb_substr($post_content,0,$int);
		$post_content = str_replace(array("\r", "\n"), '', $post_content).$end; 
	} else { 
		$post_content = str_replace(array("\r", "\n"), '', $post_content);
	}
	return $post_content;
}

##記事文章の自動整形停止

functions.php
add_action('init', function() {
    remove_filter('the_title', 'wptexturize');
    remove_filter('the_content', 'wptexturize');
    remove_filter('the_excerpt', 'wptexturize');
    remove_filter('the_title', 'wpautop');
    remove_filter('the_content', 'wpautop');
    remove_filter('the_excerpt', 'wpautop');
    remove_filter('the_editor_content', 'wp_richedit_pre');
});

##絵文字スクリプト削除

functions.php
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' );

* page-***.php の階層を持たせるフィルター

functions.php
function my_page_template ( $template ){
	global $post;
	$post_type = get_post_type_object($post->post_type);
	if ( $post_type->hierarchical ){
		$slug = get_page_uri($post->ID);
		$slug = str_replace( '/', '-', $slug );
		$buf_template = locate_template('page-' . $slug . '.php');
		$buf_page_template = get_page_template_slug();
		if ( !empty($buf_template) && empty($buf_page_template) ){
			$template = $buf_template;
		}
	}
	return $template;
}
add_filter( 'page_template', 'my_page_template' );

##アドバンスカスタムフィールドの「画像」を扱う便利な関数
■ACF画像の呼び出し
ACF_image('項目名','サイズ','種類');
種類:photo、url、alt、title、caption
※種類とサイズは省略可能。
※ACF画像の返り値は必ず「画像オブジェクト」にする。

functions.php
function ACF_img($str,$size_name='full',$type='photo',$row=''){

	//空入力を有効に
	if($type ==''){$type = 'photo';}

	//rowを第2因数以降でも有効に
	if($size_name == 'row' || $type == 'row' ){
		$row = 'row';
		$type='photo';
		if($size_name == 'row'){
			$size_name='full';
		}
	}

	//rowの処理
	if($row != 'row'){
		$image = get_field($str);
	}else{
		//繰り返し(repeater)の画像呼び出し
		$image = get_sub_field($str);
	}

	//画像情報の読み込み
	if( !empty($image) ){
		// vars
		$url = $image['url'];
		$alt = $image['alt'];
		$title = $image['title'];
		$caption = $image['caption'];

		// Resize
		if(($size_name != '') && ($size_name != 'full')){
			$thumb = $image['sizes'][$size_name];
		}else{
			$thumb = $url;
		}

		switch ($type){
			case 'photo': 	$photo = '<img src="'.$thumb.'" alt="'.$alt.'" />';break;
			case 'url': 	$photo = $thumb;break;
			case 'alt': 	$photo = $image['alt'];break;
			case 'title': 	$photo = $image['title'];break;
			case 'caption': $photo = $image['caption'];break;
		}

		echo $photo;

	}

}
16
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
20