WordPress

WordPress★ページごとに個別のCSSを読み込ませる方法★編集画面下にCSSを入力するエリアをつくる

投稿日:2019年11月26日 更新日:

WordPressはCSSを「style.css」に記述していくのが一般的な方法。
一つのファイルにまとめて書くとものすごい量になってしまうので、もっとスッキリさせたい!
ページごとに読み込む方法はいくつかありますが、先日使った「emanon」というテーマで、新規ページの編集画面に個別のCSSを書き込む欄があり、とても便利だったので作り方を調べてみた。

やり方は、functions.phpをカスタマイズします。
下の2つの方法が良さそうでした。

ヘッダーに個別のCSSファイルを読み込む方法
https://shufulife.com/addtohead/

ヘッダーにファイルを読み込む+ページに書き込む欄を作る方法
https://oku-log.com/blog/page-custom/

今回は、「ヘッダーにファイルを読み込む+ページに書き込む欄を作る方法」を試してみました。
ヘッダーにファイルを読み込ませる場合は、フルパスで下のように記述。

<link rel="stylesheet" href="http://test.com/wp-content/themes/css/wp.css">

 

チョー快適に、サクサク動いてます!
いつもながら、作者さまに感謝!!

★コードを転載。

//カスタムCSSエリア
add_action('admin_menu', 'custom_css_hooks');
add_action('save_post', 'save_custom_css');
add_action('wp_head','insert_custom_css');
function custom_css_hooks() {
	add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'post', 'normal', 'high');
	add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'page', 'normal', 'high');
}
function custom_css_input() {
	global $post;
	echo '<input type="hidden" name="custom_css_noncename" id="custom_css_noncename" value="'.wp_create_nonce('custom-css').'" />';
	echo '<textarea name="custom_css" id="custom_css" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_css',true).'</textarea>';
}
function save_custom_css($post_id) {
	if (!wp_verify_nonce($_POST['custom_css_noncename'], 'custom-css')) return $post_id;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
	$custom_css = $_POST['custom_css'];
	update_post_meta($post_id, '_custom_css', $custom_css);
}
function insert_custom_css() {
	if (is_page() || is_single()) {
	if (have_posts()) : while (have_posts()) : the_post();
	echo '<style type="text/css">'.get_post_meta(get_the_ID(), '_custom_css', true).'</style>';
	endwhile; endif;
	rewind_posts();
	}
}

//カスタムJSエリア
add_action( 'admin_menu', 'custom_js_hooks' );
add_action( 'save_post', 'save_custom_js' );
add_action( 'wp_head','insert_custom_js' );
function custom_js_hooks() {
	add_meta_box( 'custom_js', 'Custom JS', 'custom_js_input', 'post', 'normal', 'high' );
	add_meta_box( 'custom_js', 'Custom JS', 'custom_js_input', 'page', 'normal', 'high' );
}
function custom_js_input() {
	global $post;
	echo '<input type="hidden" name="custom_js_noncename" id="custom_js_noncename" value="'.wp_create_nonce('custom-js').'" />';
	echo '<textarea name="custom_js" id="custom_js" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_js',true).'</textarea>';
}
function save_custom_js($post_id) {
	if (!wp_verify_nonce($_POST['custom_js_noncename'], 'custom-js')) return $post_id;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
	$custom_js = $_POST['custom_js'];
	update_post_meta($post_id, '_custom_js', $custom_js);
}
function insert_custom_js() {
	if ( is_page() || is_single() ) {
	if ( have_posts() ) : while ( have_posts() ) : the_post();
		echo '<script type="text/javascript">' . get_post_meta(get_the_ID(), '_custom_js', true) . '</script>';
	endwhile; endif;
	rewind_posts();
	}
}

//カスタムhead内タグ追加
add_action('admin_menu', 'add_head_hooks');
add_action('save_post', 'save_add_head');
add_action('wp_head','insert_add_head');
function add_head_hooks() {
	add_meta_box('add_head', 'head内タグ追加', 'add_head_input', 'post', 'normal', 'high');
	add_meta_box('add_head', 'head内タグ追加', 'add_head_input', 'page', 'normal', 'high');
}
function add_head_input() {
	global $post;
	echo '<input type="hidden" name="add_head_noncename" id="add_head_noncename" value="'.wp_create_nonce('add-head').'" />';
	echo '<textarea name="add_head" id="add_head" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_add_head',true).'</textarea>';
}
function save_add_head($post_id) {
	if (!wp_verify_nonce($_POST['add_head_noncename'], 'add-head')) return $post_id;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
	$add_head = $_POST['add_head'];
	update_post_meta($post_id, '_add_head', $add_head);
}
function insert_add_head() {
	if (is_page() || is_single()) {
		if (have_posts()) : while (have_posts()) : the_post();
			echo get_post_meta(get_the_ID(), '_add_head', true);
		endwhile; endif;
		rewind_posts();
	}
}

 

参考にさせていただいたサイト:https://shufulife.com/addtohead/

本サイトの内容に誤りや不正確な記述がある場合やサンプルに基づくいかなる結果も一切の責任を負いかねますので、あらかじめご了承ください。

-WordPress

執筆者:


comment

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

関連記事

WP★吹き出し風デザインをCSSだけでやる方法

ワードプレスでプラグインを使わずに、 HTMLとCSSだけで下のような感じの会話風レイアウトの作り方。   手順はこちら   [codepen_embed height=”400 …

Binoculars Telescope Watch Beach  - analogicus / Pixabay

WordPrss★検索結果を表示するテンプレートをsearch.phpから任意の○○○.phpファイルにするには

WordPressの基本の検索ファイル WordPressはテンプレート階層の優先順位の高い順番にファイルを探して、 最初に見つかったテンプレートファイルを使ってページを表示します。WordPress …

WordPrss★テンプレートを投稿記事ごとに替える

テンプレートを投稿記事ごとに替える 固定ページは、phpファイルを作っておけばテンプレートとして選べるようになるけれど、投稿ページで、テンプレートを作るにはどうしたらいいのだろう。。。 WordPre …

WordPress★Contact Form 7 で隠しフィールドを設定する

Contact Form 7 と Flamingo のセットで使用中。 Flamingo側の受信メッセージの一覧表示で使うために、送信元となるContact Form 7 に隠しフィールドを設定するこ …

WP★タイトルを大文字から小文字に直す(Twenty Seventeen)

  WordPressのテーマ『Twenty Seventeen』で、ページのタイトルが大文字に強制的に変換されてしまうのをなくす方法です。今さら…ですが、最近手直ししたので覚え書き。 &n …

サイト制作・グラフィック制作をしているデザイナーです。制作で躓いたことの備忘録としてこのブログを始めました。
たくさんの検索結果から、実際に何度も何度も自分で試してみて、自分に合うなと思った方法やうまくいった方法をこのブログに書き留めています。
プログラム関係のことはもっぱらコピペ派。
自分でゴリゴリ書くことはできません。。。プログラムが分かる人がうらやましいです。
そんな私でもなんとかサイト制作ができるのはグーグル先生と貴重な情報をおしげもなく公開してくださっているたくさんの方々のおかげ。
有志のみなさまに日々感謝しつつ制作させていただいています。