Advertisement
Guest User

Untitled

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