WEBOPIXEL

WordPressのカスタムフィールドをコードベースで追加できるライブラリ「Custom Metaboxes and Fields for WordPress」

Posted: 2014.05.22 / Category: WordPress / Tag: 

WordPressのカスタムフィールドプラグインはGUIでお手軽に作成できるのもありますが、個人的にはコードで作成できた方が使い回しができたりするので好んでます。
「Custom Metaboxes and Fields for WordPress」はGitHubで公開されていて更新も活発そうだったので使用してみました。

Sponsored Link

【動作環境】
WordPress 3.8.1
Custom Metaboxes and Fields for WordPress 1.0.1

ダウンロード

下記からダウンロードしてください。

Custom Metaboxes and Fields for WordPress

ダウンロードしたら、現在使用しているテーマフォルダにいれて、フォルダ名を「metabox」に変更します。

テキストフィールドの追加

まずは簡単なテキストフィールドを追加してみましょう。
「functions.php」で下記を記述してダウンロードした「metabox」フォルダの中の「init.php」を読み込みます。

functions.php

	function cmb_initialize_cmb_meta_boxes() {
		if (!class_exists('cmb_Meta_Box'))
			require_once 'metabox/init.php';
	}
	add_action('init', 'cmb_initialize_cmb_meta_boxes', 9999);
	

これでライブラリを使用する準備ができました。
続いて下記を追記します。

functions.php

	function file_metaboxes(array $meta_boxes) {
		$prefix = '_cmb_';
		$meta_boxes['test_metabox'] = array(
			'id'         => 'test_metabox',
			'title'      => '追加したカスタムフィールド',
			'pages'      => array('post'),
			'context'    => 'normal',
			'priority'   => 'high',
			'show_names' => true,
			'fields'     => array(
				array(
					'name' => 'テストテキスト',
					'desc' => 'ここに説明文が入ります。',
					'id'   => $prefix . 'test_text',
					'type' => 'text'
				),
			)
		);
		return $meta_boxes;
	}
	add_filter( 'cmb_meta_boxes', 'file_metaboxes' );
	

テキストフィールドを追加した

テキストフィールド以外にもセレクトボックスやファイルなどひと通り揃ってます。
詳しくは下記ページで確認してください。

Field Types

複数のフィールドを追加

フィールドを複数追加するには「fields」部分に配列で追加していくだけですね。

functions.php

	'fields'     => array(
		array(
			'name' => 'テストテキスト',
			'desc' => 'ここに説明文が入ります。',
			'id'   => $prefix . 'test_text',
			'type' => 'text',
		),
		array(
			'name' => 'ファイル',
			'desc' => 'ファイルをリンクする場合はここで選択してください。',
			'id' => $prefix . 'test_file',
			'type' => 'file',
			'save_id' => false,
			'allow' => array( 'url', 'attachment' )
		),
	)
	

フィールドをリピートにする

入力欄の数を管理画面で自由に追加できるようにする設定です。
個別のフィールドをリピートさせたいときは「repeatable」オプションを追加するだけです。

functions.php

	'fields'     => array(
		array(
			'name' => 'テストテキスト',
			'desc' => 'ここに説明文が入ります。',
			'id'   => $prefix . 'test_text',
			'type' => 'text',
			'repeatable' => true,
		),
	)
	

グループをリピートする

たとえばテキストと画像を一つのセットとしてリピートしたい場合があるとします。
そういうときは「fields」の「type」を「group」にして、さらに「fields」を指定すればひとつのグループとして扱えます。

functions.php

	$meta_boxes['test_metabox'] = array(
		'id'         => 'test_metabox',
		'title'      => '追加したカスタムフィールド',
		'pages'      => array('post'),
		'context'    => 'normal',
		'priority'   => 'high',
		'show_names' => true,
		'fields'     => array(
			array(
				'id'          => $prefix . 'repeat_group',
				'type'        => 'group',
				'options'     => array(
					'add_button'    => 'グループの追加',
					'remove_button' => 'グループの削除',
					'sortable'      => true, // beta
				),
				'fields'     => array(
					array(
						'name' => 'テストテキスト',
						'desc' => 'ここに説明文が入ります。',
						'id'   => $prefix . 'test_text',
						'type' => 'text',
					),
					array(
						'name' => 'ファイル',
						'desc' => 'ファイルをリンクする場合はここで選択してください。',
						'id' => $prefix . 'add_file',
						'type' => 'file',
						'save_id' => false,
						'allow' => array( 'url', 'attachment' )
					),
				)
			)
		)
	);
	

リピートするグループフィールド

その他拡張機能など

拡張機能として「Google Map」があったり「Select2」でセレクトボックスを選択しやすくできたりします。