【動作環境】
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

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

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

functions.php

  1. function file_metaboxes(array $meta_boxes) {
  2. $prefix = '_cmb_';
  3. $meta_boxes['test_metabox'] = array(
  4. 'id' => 'test_metabox',
  5. 'title' => '追加したカスタムフィールド',
  6. 'pages' => array('post'),
  7. 'context' => 'normal',
  8. 'priority' => 'high',
  9. 'show_names' => true,
  10. 'fields' => array(
  11. array(
  12. 'name' => 'テストテキスト',
  13. 'desc' => 'ここに説明文が入ります。',
  14. 'id' => $prefix . 'test_text',
  15. 'type' => 'text'
  16. ),
  17. )
  18. );
  19. return $meta_boxes;
  20. }
  21. add_filter( 'cmb_meta_boxes', 'file_metaboxes' );

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

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

Field Types

複数のフィールドを追加

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

functions.php

  1. 'fields' => array(
  2. array(
  3. 'name' => 'テストテキスト',
  4. 'desc' => 'ここに説明文が入ります。',
  5. 'id' => $prefix . 'test_text',
  6. 'type' => 'text',
  7. ),
  8. array(
  9. 'name' => 'ファイル',
  10. 'desc' => 'ファイルをリンクする場合はここで選択してください。',
  11. 'id' => $prefix . 'test_file',
  12. 'type' => 'file',
  13. 'save_id' => false,
  14. 'allow' => array( 'url', 'attachment' )
  15. ),
  16. )

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

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

functions.php

  1. 'fields' => array(
  2. array(
  3. 'name' => 'テストテキスト',
  4. 'desc' => 'ここに説明文が入ります。',
  5. 'id' => $prefix . 'test_text',
  6. 'type' => 'text',
  7. 'repeatable' => true,
  8. ),
  9. )

グループをリピートする

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

functions.php

  1. $meta_boxes['test_metabox'] = array(
  2. 'id' => 'test_metabox',
  3. 'title' => '追加したカスタムフィールド',
  4. 'pages' => array('post'),
  5. 'context' => 'normal',
  6. 'priority' => 'high',
  7. 'show_names' => true,
  8. 'fields' => array(
  9. array(
  10. 'id' => $prefix . 'repeat_group',
  11. 'type' => 'group',
  12. 'options' => array(
  13. 'add_button' => 'グループの追加',
  14. 'remove_button' => 'グループの削除',
  15. 'sortable' => true, // beta
  16. ),
  17. 'fields' => array(
  18. array(
  19. 'name' => 'テストテキスト',
  20. 'desc' => 'ここに説明文が入ります。',
  21. 'id' => $prefix . 'test_text',
  22. 'type' => 'text',
  23. ),
  24. array(
  25. 'name' => 'ファイル',
  26. 'desc' => 'ファイルをリンクする場合はここで選択してください。',
  27. 'id' => $prefix . 'add_file',
  28. 'type' => 'file',
  29. 'save_id' => false,
  30. 'allow' => array( 'url', 'attachment' )
  31. ),
  32. )
  33. )
  34. )
  35. );

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

その他拡張機能など

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