Title: 設定 API の使用
Author: Aki Hamano
Published: 2025年5月9日

---

# 設定 API の使用

## この投稿内

 * [設定の追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e8%a8%ad%e5%ae%9a%e3%81%ae%e8%bf%bd%e5%8a%a0)
    - [設定の追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e8%a8%ad%e5%ae%9a%e3%81%ae%e8%bf%bd%e5%8a%a0-2)
    - [セクションの追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e3%82%bb%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%ae%e8%bf%bd%e5%8a%a0)
    - [フィールドの追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ae%e8%bf%bd%e5%8a%a0)
    - [例](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e4%be%8b)
 * [設定の取得](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e8%a8%ad%e5%ae%9a%e3%81%ae%e5%8f%96%e5%be%97)
    - [例](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e4%be%8b-2)

[↑ ページの先頭へ戻る](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#wp--skip-link--target)

## 󠀁[設定の追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e8%a8%ad%e5%ae%9a%e3%81%ae%e8%bf%bd%e5%8a%a0)󠁿

[`register_setting()`](https://developer.wordpress.org/reference/functions/register_setting/)
を使って新しい設定を定義すると、テーブル `{$wpdb->prefix}_options` にエントリーが
作成されます。

既存のページに新しいセクションを追加するには、[`add_settings_section()`](https://developer.wordpress.org/reference/functions/add_settings_section/)
を使います。

既存のセクションに新しいフィールドを追加するには、[`add_settings_field()`](https://developer.wordpress.org/reference/functions/add_settings_field/)
を使います。

[`register_setting()`](https://developer.wordpress.org/reference/functions/register_setting/)
と同様に、前述の関数 `add_settings_*()` もすべてアクションフック `admin_init` に
追加されるべきです。

### 󠀁[設定の追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e8%a8%ad%e5%ae%9a%e3%81%ae%e8%bf%bd%e5%8a%a0-2)󠁿

    ```notranslate
    register_setting(
      string $option_group,
      string $option_name,
      callable $sanitize_callback = ''
    );
    ```

使用されるパラメータについての完全な説明は、[`register_setting()`](https://developer.wordpress.org/reference/functions/register_setting/)
に関する関数リファレンスを参照してください。

### 󠀁[セクションの追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e3%82%bb%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%ae%e8%bf%bd%e5%8a%a0)󠁿

    ```notranslate
    add_settings_section(
      string $id,
      string $title,
      callable $callback,
      string $page
    );
    ```

セクションとは、WordPress の設定ページで見られる、共通の見出しを持つ設定グループ
のことです。プラグインでは、まったく新しいページを作成するのではなく、既存の設定
ページに新しいセクションを追加できます。これにより、プラグインのメンテナンスが簡単
になり、ユーザーが学習するための新しいページが少なくなります。

使用されるパラメータについての完全な説明は、[`add_settings_section()`](https://developer.wordpress.org/reference/functions/add_settings_section/)
に関する関数リファレンスを参照してください。

### 󠀁[フィールドの追加](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e3%83%95%e3%82%a3%e3%83%bc%e3%83%ab%e3%83%89%e3%81%ae%e8%bf%bd%e5%8a%a0)󠁿

    ```notranslate
    add_settings_field(
      string $id,
      string $title,
      callable $callback,
      string $page,
      string $section = 'default',
      array $args = []
    );
    ```

使用されるパラメータについての完全な説明は、[`add_settings_field()`](https://developer.wordpress.org/reference/functions/add_settings_field/)
に関する関数リファレンスを参照してください。

### 󠀁[例](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e4%be%8b)󠁿

    ```notranslate
    function wporg_settings_init() {
      // register a new setting for "reading" page
      register_setting('reading', 'wporg_setting_name');

      // register a new section in the "reading" page
      add_settings_section(
        'wporg_settings_section',
        'WPOrg Settings Section', 'wporg_settings_section_callback',
        'reading'
      );

      // register a new field in the "wporg_settings_section" section, inside the "reading" page
      add_settings_field(
        'wporg_settings_field',
        'WPOrg Setting', 'wporg_settings_field_callback',
        'reading',
        'wporg_settings_section'
      );
    }

    /**
     * register wporg_settings_init to the admin_init action hook
     */
    add_action('admin_init', 'wporg_settings_init');

    /**
     * callback functions
     */

    // section content cb
    function wporg_settings_section_callback() {
      echo '<p>WPOrg Section Introduction.</p>';
    }

    // field content cb
    function wporg_settings_field_callback() {
      // get the value of the setting we've registered with register_setting()
      $setting = get_option('wporg_setting_name');
      // output the field
      ?>
      <input type="text" name="wporg_setting_name" value="<?php echo isset( $setting ) ? esc_attr( $setting ) : ''; ?>">
      <?php
    }
    ```

## 󠀁[設定の取得](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e8%a8%ad%e5%ae%9a%e3%81%ae%e5%8f%96%e5%be%97)󠁿

    ```notranslate
    get_option(
      string $option,
      mixed $default = false
    );
    ```

設定の取得は、関数 [`get_option()`](https://developer.wordpress.org/reference/functions/get_option/)
で実現します。

この関数は2つのパラメータを受け付けます。オプションの名前と、そのオプションに対する
省略可能なデフォルト値です。

### 󠀁[例](https://ja.wordpress.org/team/handbook/plugin-development/settings/using-settings-api/?output_format=md#%e4%be%8b-2)󠁿

    ```notranslate
    // Get the value of the setting we've registered with register_setting()
    $setting = get_option('wporg_setting_name');
    ```

[原文](https://developer.wordpress.org/plugins/settings/using-settings-api/) / [日本語訳](https://github.com/jawordpressorg/developer-plugins-handbook/blob/main/settings/using-settings-api/index.md)

公開日

2025年5月9日

最終更新

2025年5月9日

[  前へ: 設定 API](https://ja.wordpress.org/team/handbook/plugin-development/settings/settings-api/)

[  次へ: メタデータ](https://ja.wordpress.org/team/handbook/plugin-development/metadata/)