LoginSignup
34
37

More than 5 years have passed since last update.

WordPressでちょっとした管理ページを作成するコード

Posted at

ダッシュボードの設定ページを簡易的に作成するコード

WordPressでテーマにちょっとした管理用の項目を設定したいときってありますよね。専用のプラグインをつくったりとか大げさにしなくても、ちょっとしたオプション項目を2〜3個だけ設定したい時のための、簡単な管理ページ設定方法。

参考にしたのは

設定ページの作成(Codex)

こちらのページで紹介されているSettings APIをつかって、汎用的に設定ページを作成するコードを書いてみました。
functions.php に貼り付けて使います。

管理ページ作成するコード

functions.php
class mySettingPage{
  public static $key='my_option_name';//オプションへの保存、呼び出しキー

  private $html_title='HTMLタグのtitle';//HTMLのタイトル(管理ページのtitleタグ)
  private $page_title='ページのタイトル';//ページのタイトル
  private $page_slug='my_option_page';

  private $options;
  private $group='my_option_group';
  private $section='my_setting_admin';

  //ここにキーとタイトル、コールバックをセットにして
  public static function getFields(){

    return array(
      array(
        'type'=>'section',//区切りを入れるときはtypeをセクションにする
        'name'=>'section1',
        'title'=>'入力してください',
        'callback'=>'section_callback',
      ),
      array(
        'type'=>'field',//フィールドかセクション お好みに合わせて追加
        'name'=>'item1',//名前
        'title'=>'項目1',//タイトル
        'callback'=>'text_callback',//コールバック
      ),
      array(
        'type'=>'field',
        'name'=>'tel',
        'title'=>'電話',
        'callback'=>'text_callback',
      ),
      array(
        'type'=>'section',//区切りを入れるときはtypeをセクションにする
        'name'=>'section2',
        'title'=>'',
        'callback'=>'section_callback',
      ),
      array(
        'type'=>'field',
        'name'=>'address',
        'title'=>'住所',
        'callback'=>'text_callback',
      ),
    );
  }

  //初期化
  public function __construct(){
      add_action('admin_menu',array($this,'add_my_option_page'));
      add_action('admin_init',array($this,'page_init'));
  }

  //キーを取得(外部から呼び出せるようにする)
  public static function getKey(){
    return self::$key;
  }

  //設定
  public function add_my_option_page(){
      add_options_page(
          $this->html_title,//ダッシュボードのメニューに表示するテキスト
          $this->page_title,//ページのタイトル
          'edit_themes',
          $this->page_slug,//ページスラッグ
          array( $this, 'create_admin_page' )
      );
  }

  //フォームの外観作成
  public function create_admin_page(){
      // Set class property
      $this->options = get_option($this->getKey());
      ?>
      <div class="wrap">
          <?php screen_icon(); ?>
          <h2><?php echo $this->page_title;?></h2>
          <p>テーマ用の設定項目</p>
          <form method="post" action="options.php">
          <?php
              // This prints out all hidden setting fields
              settings_fields($this->group);
              do_settings_sections($this->section);
              submit_button();
          ?>
          </form>
      </div>
      <?php
  }

  //フォームの部品組み立て
  public function page_init(){
    register_setting(
      $this->group, // Option group
      $this->getKey(), // Option name
      array( $this, 'sanitize' ) // Sanitize
    );

    $fields=$this->getFields();
    $section_id='';
    foreach($fields AS $field){
      if($field['type']=='field'){
        add_settings_section(
          $field['name'], // ID
          $field['title'], // Title
          array($this,$field['callback']), // Callback
          $this->section, // Page
          $section_id
        );
      }else{
        add_settings_section(
          $field['name'], // ID
          $field['title'], // Title
          array($this,$field['callback']), // Callback
          $this->section // Page
        );
        $section_id=$field['name'];
      }
    }
  }

  //保存前のサニタイズ
  public function sanitize($input){

    $new_input = array();
    foreach($this->getFields() AS $field){
      if(isset($input[$field['name']])){
        $new_input[$field['name']] = sanitize_text_field($input[$field['name']]);
      }
    }
    return $new_input;
  }

  //セクション表示関数
  public function section_callback(array $args){
    echo '<hr>';
  }

  //テキストフィール表示関数
  public function text_callback(array $args){
    $name=$args['id'];
    printf(
      '<input type="text" id="'.$name.'" name="'.$this->getKey().'['.$name.']" value="%s" />',
      isset( $this->options[$name] ) ? esc_attr( $this->options[$name]) : ''
    );
  }


}

if(is_admin())
    $my_settings_page = new mySettingPage();

使い方

  1. 上記コードをfunctions.phpに貼り付け
  2. html_titleとpage_titleを変更
  3. getFieldsの中身を変更
  4. その他必要に応じて書き変えてください
  5. これでダッシュボードメニューの設定の下に設定ページが出来上がります

こんな感じのが出来上がります

スクリーンショット 2015-09-29 9.51.29.png

登録した項目を使う

下記コードで呼び出し
<?php $options=get_option(mySettingPage::getKey());?>

表示させるところで、
<?php echo esc_html($options['item1']);?>
<?php echo esc_html($options['tel']);?>
<?php echo esc_html($options['address']);?>

非常に簡単な内容ですが、テーマをつくっているとたまにテーマ独自の値があるといいなとか思うことがあるのでそんなときに。

参考サイト

設定ページの作成(Codex)
add_settings_fieldのコールバックの引数

34
37
0

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
34
37