Wordpress Theme Options Page


/ Published in: PHP
Save to your folder(s)

This creates a theme options page for a wordpress theme.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /* This should be saved as theme-options.php and
  4.  * you'll need to use the following in your functions.php:
  5.  * require_once ( get_stylesheet_directory() . '/theme-options.php' );
  6.  */
  7.  
  8. add_action( 'admin_init', 'theme_options_init' );
  9. add_action( 'admin_menu', 'theme_options_add_page' );
  10.  
  11. /**
  12.  * Init plugin options to white list our options
  13.  */
  14. function theme_options_init(){
  15. register_setting( 'sample_options', 'sample_theme_options', 'theme_options_validate' );
  16. }
  17.  
  18. /**
  19.  * Load up the menu page
  20.  */
  21. function theme_options_add_page() {
  22. add_theme_page( __( 'Theme Options', 'sampletheme' ), __( 'Theme Options', 'sampletheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
  23. }
  24.  
  25. /**
  26.  * Create arrays for our select and radio options
  27.  */
  28. $select_options = array(
  29. '0' => array(
  30. 'value' => '0',
  31. 'label' => __( 'Zero', 'sampletheme' )
  32. ),
  33. '1' => array(
  34. 'value' => '1',
  35. 'label' => __( 'One', 'sampletheme' )
  36. ),
  37. '2' => array(
  38. 'value' => '2',
  39. 'label' => __( 'Two', 'sampletheme' )
  40. ),
  41. '3' => array(
  42. 'value' => '3',
  43. 'label' => __( 'Three', 'sampletheme' )
  44. ),
  45. '4' => array(
  46. 'value' => '4',
  47. 'label' => __( 'Four', 'sampletheme' )
  48. ),
  49. '5' => array(
  50. 'value' => '3',
  51. 'label' => __( 'Five', 'sampletheme' )
  52. )
  53. );
  54.  
  55. $radio_options = array(
  56. 'yes' => array(
  57. 'value' => 'yes',
  58. 'label' => __( 'Yes', 'sampletheme' )
  59. ),
  60. 'no' => array(
  61. 'value' => 'no',
  62. 'label' => __( 'No', 'sampletheme' )
  63. ),
  64. 'maybe' => array(
  65. 'value' => 'maybe',
  66. 'label' => __( 'Maybe', 'sampletheme' )
  67. )
  68. );
  69.  
  70. /**
  71.  * Create the options page
  72.  */
  73. function theme_options_do_page() {
  74. global $select_options, $radio_options;
  75.  
  76. if ( ! isset( $_REQUEST['settings-updated'] ) )
  77. $_REQUEST['settings-updated'] = false;
  78.  
  79. ?>
  80. <div class="wrap">
  81. <?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'sampletheme' ) . "</h2>"; ?>
  82.  
  83. <?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
  84. <div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div>
  85. <?php endif; ?>
  86.  
  87. <form method="post" action="options.php">
  88. <?php settings_fields( 'sample_options' ); ?>
  89. <?php $options = get_option( 'sample_theme_options' ); ?>
  90.  
  91. <table class="form-table">
  92.  
  93. <?php
  94. /**
  95. * A sample checkbox option
  96. */
  97. ?>
  98. <tr valign="top"><th scope="row"><?php _e( 'A checkbox', 'sampletheme' ); ?></th>
  99. <td>
  100. <input id="sample_theme_options[option1]" name="sample_theme_options[option1]" type="checkbox" value="1" <?php checked( '1', $options['option1'] ); ?> />
  101. <label class="description" for="sample_theme_options[option1]"><?php _e( 'Sample checkbox', 'sampletheme' ); ?></label>
  102. </td>
  103. </tr>
  104.  
  105. <?php
  106. /**
  107. * A sample text input option
  108. */
  109. ?>
  110. <tr valign="top"><th scope="row"><?php _e( 'Some text', 'sampletheme' ); ?></th>
  111. <td>
  112. <input id="sample_theme_options[sometext]" class="regular-text" type="text" name="sample_theme_options[sometext]" value="<?php esc_attr_e( $options['sometext'] ); ?>" />
  113. <label class="description" for="sample_theme_options[sometext]"><?php _e( 'Sample text input', 'sampletheme' ); ?></label>
  114. </td>
  115. </tr>
  116.  
  117. <?php
  118. /**
  119. * A sample select input option
  120. */
  121. ?>
  122. <tr valign="top"><th scope="row"><?php _e( 'Select input', 'sampletheme' ); ?></th>
  123. <td>
  124. <select name="sample_theme_options[selectinput]">
  125. <?php
  126. $selected = $options['selectinput'];
  127. $p = '';
  128. $r = '';
  129.  
  130. foreach ( $select_options as $option ) {
  131. $label = $option['label'];
  132. if ( $selected == $option['value'] ) // Make default first in list
  133. $p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";
  134. else
  135. $r .= "\n\t<option style=\"padding-right: 10px;\" value='" . esc_attr( $option['value'] ) . "'>$label</option>";
  136. }
  137. echo $p . $r;
  138. ?>
  139. </select>
  140. <label class="description" for="sample_theme_options[selectinput]"><?php _e( 'Sample select input', 'sampletheme' ); ?></label>
  141. </td>
  142. </tr>
  143.  
  144. <?php
  145. /**
  146. * A sample of radio buttons
  147. */
  148. ?>
  149. <tr valign="top"><th scope="row"><?php _e( 'Radio buttons', 'sampletheme' ); ?></th>
  150. <td>
  151. <fieldset><legend class="screen-reader-text"><span><?php _e( 'Radio buttons', 'sampletheme' ); ?></span></legend>
  152. <?php
  153. if ( ! isset( $checked ) )
  154. $checked = '';
  155. foreach ( $radio_options as $option ) {
  156. $radio_setting = $options['radioinput'];
  157.  
  158. if ( '' != $radio_setting ) {
  159. if ( $options['radioinput'] == $option['value'] ) {
  160. $checked = "checked=\"checked\"";
  161. } else {
  162. $checked = '';
  163. }
  164. }
  165. ?>
  166. <label class="description"><input type="radio" name="sample_theme_options[radioinput]" value="<?php esc_attr_e( $option['value'] ); ?>" <?php echo $checked; ?> /> <?php echo $option['label']; ?></label><br />
  167. <?php
  168. }
  169. ?>
  170. </fieldset>
  171. </td>
  172. </tr>
  173.  
  174. <?php
  175. /**
  176. * A sample textarea option
  177. */
  178. ?>
  179. <tr valign="top"><th scope="row"><?php _e( 'A textbox', 'sampletheme' ); ?></th>
  180. <td>
  181. <textarea id="sample_theme_options[sometextarea]" class="large-text" cols="50" rows="10" name="sample_theme_options[sometextarea]"><?php echo esc_textarea( $options['sometextarea'] ); ?></textarea>
  182. <label class="description" for="sample_theme_options[sometextarea]"><?php _e( 'Sample text box', 'sampletheme' ); ?></label>
  183. </td>
  184. </tr>
  185. </table>
  186.  
  187. <p class="submit">
  188. <input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'sampletheme' ); ?>" />
  189. </p>
  190. </form>
  191. </div>
  192. <?php
  193. }
  194.  
  195. /**
  196.  * Sanitize and validate input. Accepts an array, return a sanitized array.
  197.  */
  198. function theme_options_validate( $input ) {
  199. global $select_options, $radio_options;
  200.  
  201. // Our checkbox value is either 0 or 1
  202. if ( ! isset( $input['option1'] ) )
  203. $input['option1'] = null;
  204. $input['option1'] = ( $input['option1'] == 1 ? 1 : 0 );
  205.  
  206. // Say our text option must be safe text with no HTML tags
  207. $input['sometext'] = wp_filter_nohtml_kses( $input['sometext'] );
  208.  
  209. // Our select option must actually be in our array of select options
  210. if ( ! array_key_exists( $input['selectinput'], $select_options ) )
  211. $input['selectinput'] = null;
  212.  
  213. // Our radio option must actually be in our array of radio options
  214. if ( ! isset( $input['radioinput'] ) )
  215. $input['radioinput'] = null;
  216. if ( ! array_key_exists( $input['radioinput'], $radio_options ) )
  217. $input['radioinput'] = null;
  218.  
  219. // Say our textarea option must be safe text with the allowed tags for posts
  220. $input['sometextarea'] = wp_filter_post_kses( $input['sometextarea'] );
  221.  
  222. return $input;
  223. }
  224.  
  225. // adapted from http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/

URL: http://themeshaper.com/2010/06/03/sample-theme-options/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.