5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WordPressの管理画面でJSとCSSを適用する方法

Posted at

参考:WordPress の管理画面でカスタムJSの読込

手順1

自分が使っているテーマのディレクトリにwp-adminというディレクトリを作成

手順2

wp-adminの中に使いたいjsファイルとcssファイルを入れる。
ここではhoge.jsとfuga.cssをいれた。

手順3

テーマのディレクトリにあるfunction.phpに以下を追加。

function.php
function _register_custom_files() {
	$_current_theme_dir = get_template_directory_uri();
	$_custom_files = '';
	$_custom_files = _add_custom_js($_custom_files,'hoge.js');// jsを追加
	$_custom_files = _add_custom_css($_custom_files,'fuga.css');// cssを追加
	echo $_custom_files;
}
function _add_custom_js($_custom_files, $_file_name){
	$_current_theme_dir = get_template_directory_uri();
	$_custom_files .= '<script type="text/javascript" src="'
					.$_current_theme_dir
					.'/wp-admin/'
					.$_file_name
					.'"></script>';
	return $_custom_files."\n";
}
function _add_custom_css($_custom_files, $_file_name){
	$_current_theme_dir = get_template_directory_uri();
	$_custom_files .= '<link rel="stylesheet"  type="text/css" href="'
					.$_current_theme_dir
					.'/wp-admin/'
					.$_file_name
					.'" />';
	return $_custom_files."\n";
}
add_action('admin_head', '_register_custom_files');

複数追加したかったら

function.php
$_custom_files = _add_custom_js($_custom_files,'hoge.js');
$_custom_files = _add_custom_css($_custom_files,'fuga.css');

のところを下記のように追加したいファイル分増やせばいい。

function.php
$_custom_files = _add_custom_js($_custom_files,'hoge1.js');
$_custom_files = _add_custom_js($_custom_files,'hoge2.js');
$_custom_files = _add_custom_js($_custom_files,'hoge3.js');
$_custom_files = _add_custom_css($_custom_files,'fuga1.css');
$_custom_files = _add_custom_css($_custom_files,'fuga2.css');
5
8
1

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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?