Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. $options['id_number']
  2.  
  3. delete_option('my_option_name'); // regular site options
  4. delete_site_option('my_option_name'); // multisite options
  5.  
  6. $this->options = get_option( 'my_option_name' );
  7.  
  8. $this->defaults = array (
  9. 'id_number' => 123,
  10. 'title' => 'my title',
  11. );
  12.  
  13. $this->options = get_option($this->'my_option_name', $this->defaults );
  14. $this->options = wp_parse_args( $this->options, $this->defaults );
  15.  
  16. <?php
  17. class MySettingsPage
  18. {
  19. /**
  20. * Holds the values to be used in the fields callbacks
  21. */
  22. private $options;
  23.  
  24. /**
  25. * Start up
  26. */
  27. public function __construct()
  28. {
  29. add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
  30. add_action( 'admin_init', array( $this, 'page_init' ) );
  31. }
  32.  
  33. /**
  34. * Add options page
  35. */
  36. public function add_plugin_page()
  37. {
  38. // This page will be under "Settings"
  39. add_options_page(
  40. 'Settings Admin',
  41. 'My Settings',
  42. 'manage_options',
  43. 'my-setting-admin',
  44. array( $this, 'create_admin_page' )
  45. );
  46. }
  47.  
  48. /**
  49. * Options page callback
  50. */
  51. public function create_admin_page()
  52. {
  53. // Set class property
  54. $this->options = get_option( 'my_option_name' );
  55. ?>
  56. <div class="wrap">
  57. <h1>My Settings</h1>
  58. <form method="post" action="options.php">
  59. <?php
  60. // This prints out all hidden setting fields
  61. settings_fields( 'my_option_group' );
  62. do_settings_sections( 'my-setting-admin' );
  63. submit_button();
  64. ?>
  65. </form>
  66. </div>
  67. <?php
  68. }
  69.  
  70. /**
  71. * Register and add settings
  72. */
  73. public function page_init()
  74. {
  75. register_setting(
  76. 'my_option_group', // Option group
  77. 'my_option_name', // Option name
  78. array( $this, 'sanitize' ) // Sanitize
  79. );
  80.  
  81. add_settings_section(
  82. 'setting_section_id', // ID
  83. 'My Custom Settings', // Title
  84. array( $this, 'print_section_info' ), // Callback
  85. 'my-setting-admin' // Page
  86. );
  87.  
  88. add_settings_field(
  89. 'id_number', // ID
  90. 'ID Number', // Title
  91. array( $this, 'id_number_callback' ), // Callback
  92. 'my-setting-admin', // Page
  93. 'setting_section_id' // Section
  94. );
  95.  
  96. add_settings_field(
  97. 'title',
  98. 'Title',
  99. array( $this, 'title_callback' ),
  100. 'my-setting-admin',
  101. 'setting_section_id'
  102. );
  103. }
  104.  
  105. /**
  106. * Sanitize each setting field as needed
  107. *
  108. * @param array $input Contains all settings fields as array keys
  109. */
  110. public function sanitize( $input )
  111. {
  112. $new_input = array();
  113. if( isset( $input['id_number'] ) )
  114. $new_input['id_number'] = absint( $input['id_number'] );
  115.  
  116. if( isset( $input['title'] ) )
  117. $new_input['title'] = sanitize_text_field( $input['title'] );
  118.  
  119. return $new_input;
  120. }
  121.  
  122. /**
  123. * Print the Section text
  124. */
  125. public function print_section_info()
  126. {
  127. print 'Enter your settings below:';
  128. }
  129.  
  130. /**
  131. * Get the settings option array and print one of its values
  132. */
  133. public function id_number_callback()
  134. {
  135. printf(
  136. '<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',
  137. isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''
  138. );
  139. }
  140.  
  141. /**
  142. * Get the settings option array and print one of its values
  143. */
  144. public function title_callback()
  145. {
  146. printf(
  147. '<input type="text" id="title" name="my_option_name[title]" value="%s" />',
  148. isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
  149. );
  150. }
  151. }
  152.  
  153. if( is_admin() )
  154. $my_settings_page = new MySettingsPage();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement