WordPress プラグインの設定ページを作成しました。
次に、同じページに 2 つの設定セクションを作成しました。
add_plugins_page(
__('API Settings', 'api'),
__('API Settings', 'api'),
'administrator',
'api',
array('API', 'settings_display')
);
// add general settings section
add_settings_section(
'api_general',
__('General Settings', 'api'),
array('API', 'section_callback'),
'api'
);
// add page section
add_settings_section(
'api_pages',
__('Set API pages', 'api'),
array('API', 'section_callback'),
'apis'
);
その後、add_settings_field() を使用して、さまざまなフィールドがさまざまなセクションに追加されました。
ページのレンダリングを処理する関数は次のようになります。
<!-- Create a header in the default WordPress 'wrap' container -->
<div class="wrap">
<!-- Add the icon to the page -->
<?php screen_icon(); ?>
<h2><?php _e('API Settings', 'api'); ?></h2>
<!-- Make a call to the WordPress function for rendering errors when settings are saved. -->
<?php settings_errors(); ?>
<!-- Create the form that will be used to render our options -->
<form method="post" action="options.php">
<?php settings_fields('api_pages'); ?>
<?php settings_fields('api_general'); ?>
<?php do_settings_sections('api'); ?>
<?php submit_button(); ?>
</form>
</div><!-- /.wrap -->
次に、ページ設定を保存しようとすると、api_general 設定のみが保存されます。
settings_fields() は実際には、特定のセクションの nonce、action、および option_page フィールドのみを出力するためのものであることがわかりました。したがって、2 つの異なるセクションに対して呼び出した場合、1 つのセクション出力は基本的に前のセクションを無視します。
いくつかの調査を行い、チュートリアルを見てきましたが、同時に複数のセクションを保存する必要があることを示すものは見つかりませんでした。しかし、do_settings_sections() はページのすべてのセクションを出力するため、すべてのセクションを同時に保存する方法が必要です。
私の唯一の他の手段は、すべてのフィールドを同じセクションに結合するか、可能であれば避けたい別のページを作成することです.