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 を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

関連記事

WordPress★日付と時刻の書式 コピペ用にまとめてみた

日付だけでもいろいろな表示法があるので、 テーマのカスタマイズでテンプレートを編集するときの覚え書き。 日付と時刻のフォーマットと、出力結果を例 フォーマット文字列 表示   l, F j, Y Fr …

WP★テーマ開発に便利!対応テンプレートファイルがすぐにわかるプラグイン

ブロックエディターについて調べているとき偶然に、現在閲覧中のページがどのテンプレートファイルに対応しているのかすぐにわかるプラグインがあることを発見。これってWordPressでテーマのカスタマイズや …

WordPress★FaceBookページをサイトに埋め込む【2020年版】

facebook for developers ページプラグインはコチラ ページに遷移すると、下のような画面になります。 「FaceBookぺージのURL」の箇所に、埋め込みたいページのURLを貼り付 …

no image

WordPress★インストール後にやっておきたい設定

・パーマリンクの設定 方法1 「/%category%/%postname%/」のように、”カテゴリ+記事タイトル”を使う 方法2 「設定」>>「パーマリンク設定」を開きます。 「一般的な設定」欄で、 …

WordPress★「リンクを新しいタブで開く」をデフォルト設定にするプラグイン

ワードプレスで記事をいているときによく使うリンク設定。 大抵は外部リンクなので新しいウインドウで開いて欲しい。 そんなときは、いちいち歯車ボタンをおして、 設定ウインドウを出して、チェックをいれる。 …

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