Advertisement
Guest User

WP Options Page Category Selection Drop-down Menu

a guest
Mar 30th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. if ( ! function_exists( 'aqkl_get_option' ) ) :
  4. function aqkl_get_option( $option ) {
  5.     global $aqkl_options;
  6.     if( ! isset( $aqkl_options ) )
  7.         $aqkl_options = get_option( 'aqkl_theme_options', aqkl_default_options() );
  8.     return $aqkl_options[ $option ];
  9. }
  10. endif;
  11.  
  12. /* ================== STEP: 1 - default options ======================= */
  13.  
  14. if ( ! function_exists( 'aqkl_default_options' ) ) :
  15. function aqkl_default_options() {
  16.     $options = array(  
  17.        
  18.         'featured_cat' => '',
  19.     );
  20.     return $options;
  21.    
  22. /* ================== STEP: 2 - Store categories in array ======================= */
  23.  
  24. $aqkl_categories[0] = array(
  25.     'value' => 0,
  26.     'label' => ''
  27. );
  28. $aqkl_cats = get_categories(); $i = 1;
  29. foreach( $aqkl_cats as $aqkl_cat ) :
  30.     $aqkl_categories[$aqkl_cat->cat_ID] = array(
  31.         'value' => $aqkl_cat->cat_ID,
  32.         'label' => $aqkl_cat->cat_name
  33.     );
  34.     $i++;
  35. endforeach;
  36.  
  37. /* ================== STEP: 3 - Options ======================= */
  38.  
  39. function aqkl_category_settings_sections() {
  40.     add_settings_section( 'aqkl_category_options_cb', __( 'Multple Categories Options', 'themename' ), 'aqkl_category_options_cb', 'aqkl_options_link' );
  41. }
  42.  
  43. function aqkl_category_options_cb() {
  44.     add_settings_field( 'aqkl_featured_category_options', __( 'Categories Option', 'themename' ), 'aqkl_featured_category_options', 'aqkl_options_link', 'aqkl_category_options_cb' );
  45. }
  46.  
  47. function aqkl_featured_category_options() { ?>
  48.     <select id="featured_cat" name="aqkl_theme_options[featured_cat]">
  49.     <?php
  50.     foreach ( $aqkl_categories as $category ) :
  51.         $label = $category['label'];
  52.         $selected = '';
  53.         if ( $category['value'] == aqkl_get_option('featured_cat') )
  54.             $selected = 'selected="selected"';
  55.         echo '<option style="padding-right: 10px;" value="' . esc_attr( $category['value'] ) . '" ' . $selected . '>' . $label . '</option>';
  56.     endforeach;
  57.     ?>
  58.     </select>
  59. <?php
  60. }
  61.  
  62. /* ================ STEP: 4 - Sanitize ================ */
  63. function aqkl_validate_theme_options( $input ) {
  64.  
  65.     if ( isset( $input['submit-general'] ) ) {
  66.    
  67.         $prev = aqkl_get_option('featured_cat');
  68.         // We verify if the given value exists in the categories array
  69.         if ( !array_key_exists( $input['featured_cat'], $aqkl_categories ) )
  70.         $input['featured_cat'] = $prev;
  71.     }
  72.        
  73.     $input = wp_parse_args( $input, get_option( 'aqkl_theme_options', aqkl_default_options() ) );
  74.    
  75.     return $input;
  76. }
  77.  
  78. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement