完全な例

WPOrgという名前のトップレベルメニューを追加し、wporg_optionsというカスタムオプションを登録し、Settings APIおよびOptions APIを使用してCRUD(作成、読み取り、更新、削除)ロジックを実行します(エラー/更新メッセージの表示を含む)。

  1. /**
  2. * @internal never define functions inside callbacks.
  3. * these functions could be run multiple times; this would result in a fatal error.
  4. */
  5. /**
  6. * custom option and settings
  7. */
  8. function wporg_settings_init() {
  9. // Register a new setting for "wporg" page.
  10. register_setting( 'wporg', 'wporg_options' );
  11. // Register a new section in the "wporg" page.
  12. add_settings_section(
  13. 'wporg_section_developers',
  14. __( 'The Matrix has you.', 'wporg' ), 'wporg_section_developers_callback',
  15. 'wporg'
  16. );
  17. // Register a new field in the "wporg_section_developers" section, inside the "wporg" page.
  18. add_settings_field(
  19. 'wporg_field_pill', // As of WP 4.6 this value is used only internally.
  20. // Use $args' label_for to populate the id inside the callback.
  21. __( 'Pill', 'wporg' ),
  22. 'wporg_field_pill_cb',
  23. 'wporg',
  24. 'wporg_section_developers',
  25. array(
  26. 'label_for' => 'wporg_field_pill',
  27. 'class' => 'wporg_row',
  28. 'wporg_custom_data' => 'custom',
  29. )
  30. );
  31. }
  32. /**
  33. * Register our wporg_settings_init to the admin_init action hook.
  34. */
  35. add_action( 'admin_init', 'wporg_settings_init' );
  36. /**
  37. * Custom option and settings:
  38. * - callback functions
  39. */
  40. /**
  41. * Developers section callback function.
  42. *
  43. * @param array $args The settings array, defining title, id, callback.
  44. */
  45. function wporg_section_developers_callback( $args ) {
  46. ?>
  47. <p id="<?php echo esc_attr( $args['id'] ); ?>"><?php esc_html_e( 'Follow the white rabbit.', 'wporg' ); ?></p>
  48. <?php
  49. }
  50. /**
  51. * Pill field callbakc function.
  52. *
  53. * WordPress has magic interaction with the following keys: label_for, class.
  54. * - the "label_for" key value is used for the "for" attribute of the <label>.
  55. * - the "class" key value is used for the "class" attribute of the <tr> containing the field.
  56. * Note: you can add custom key value pairs to be used inside your callbacks.
  57. *
  58. * @param array $args
  59. */
  60. function wporg_field_pill_cb( $args ) {
  61. // Get the value of the setting we've registered with register_setting()
  62. $options = get_option( 'wporg_options' );
  63. ?>
  64. <select
  65. id="<?php echo esc_attr( $args['label_for'] ); ?>"
  66. data-custom="<?php echo esc_attr( $args['wporg_custom_data'] ); ?>"
  67. name="wporg_options[<?php echo esc_attr( $args['label_for'] ); ?>]">
  68. <option value="red" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'red', false ) ) : ( '' ); ?>>
  69. <?php esc_html_e( 'red pill', 'wporg' ); ?>
  70. </option>
  71. <option value="blue" <?php echo isset( $options[ $args['label_for'] ] ) ? ( selected( $options[ $args['label_for'] ], 'blue', false ) ) : ( '' ); ?>>
  72. <?php esc_html_e( 'blue pill', 'wporg' ); ?>
  73. </option>
  74. </select>
  75. <p class="description">
  76. <?php esc_html_e( 'You take the blue pill and the story ends. You wake in your bed and you believe whatever you want to believe.', 'wporg' ); ?>
  77. </p>
  78. <p class="description">
  79. <?php esc_html_e( 'You take the red pill and you stay in Wonderland and I show you how deep the rabbit-hole goes.', 'wporg' ); ?>
  80. </p>
  81. <?php
  82. }
  83. /**
  84. * Add the top level menu page.
  85. */
  86. function wporg_options_page() {
  87. add_menu_page(
  88. 'WPOrg',
  89. 'WPOrg Options',
  90. 'manage_options',
  91. 'wporg',
  92. 'wporg_options_page_html'
  93. );
  94. }
  95. /**
  96. * Register our wporg_options_page to the admin_menu action hook.
  97. */
  98. add_action( 'admin_menu', 'wporg_options_page' );
  99. /**
  100. * Top level menu callback function
  101. */
  102. function wporg_options_page_html() {
  103. // check user capabilities
  104. if ( ! current_user_can( 'manage_options' ) ) {
  105. return;
  106. }
  107. // add error/update messages
  108. // check if the user have submitted the settings
  109. // WordPress will add the "settings-updated" $_GET parameter to the url
  110. if ( isset( $_GET['settings-updated'] ) ) {
  111. // add settings saved message with the class of "updated"
  112. add_settings_error( 'wporg_messages', 'wporg_message', __( 'Settings Saved', 'wporg' ), 'updated' );
  113. }
  114. // show error/update messages
  115. settings_errors( 'wporg_messages' );
  116. ?>
  117. <div class="wrap">
  118. <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
  119. <form action="options.php" method="post">
  120. <?php
  121. // output security fields for the registered setting "wporg"
  122. settings_fields( 'wporg' );
  123. // output setting sections and their fields
  124. // (sections are registered for "wporg", each field is registered to a specific section)
  125. do_settings_sections( 'wporg' );
  126. // output save settings button
  127. submit_button( 'Save Settings' );
  128. ?>
  129. </form>
  130. </div>
  131. <?php
  132. }