php - WordPress Settings field: Saving multiple <select> options -


in wordpress options page have settings field has html select attribute of multiple. select's options dynamically populated custom post types, fine there. can save 1 value array, not more. current var_dump:

array(1) { ["awc_cpt"]=> string(12) "board_member" } 

ideally i'd array return:

array(     "board_member" => "board_member",     "another_cpt" => "another_cpt",     // , on many custom post types needed ) 

i beginner @ php, may missing fundamentals.

question: how save multiple selected options array wordpress setting?

<?php  class awc_redirect {     private $awc_redirect_options;     public $non_archived_posts = array();      public function __construct() {         if( is_admin() ){             add_action( 'admin_menu', array( $this, 'awc_redirect_add_plugin_page' ) );             add_action( 'admin_init', array( $this, 'awc_redirect_page_init' ) );         }          add_action( 'template_redirect', array( $this, 'awc_template_redirect') );     }      public function awc_redirect_add_plugin_page() {         add_options_page(             'awc redirect', // page_title             'awc redirect', // menu_title             'manage_options', // capability             'awc-redirect', // menu_slug             array( $this, 'awc_redirect_create_admin_page' ) // function         );     }      public function awc_redirect_create_admin_page() {         $this->awc_redirect_options = get_option( 'awc_redirect_option_name' ); ?>          <div class="wrap">             <h2>awc redirect</h2>             <p></p>             <?php settings_errors(); ?>              <form method="post" action="options.php">                 <?php                     settings_fields( 'awc_redirect_option_group' );                     do_settings_sections( 'awc-redirect-admin' );                     submit_button();                 ?>             </form>         </div>         <?php var_dump(get_option( 'awc_redirect_option_name' )); ?>     <?php }      public function awc_redirect_page_init() {         register_setting(             'awc_redirect_option_group', // option_group             'awc_redirect_option_name', // option_name             array( $this, 'awc_redirect_sanitize' ) // sanitize_callback         );          add_settings_section(             'awc_redirect_setting_section', // id             'settings', // title             array( $this, 'awc_redirect_section_info' ), // callback             'awc-redirect-admin' // page         );          add_settings_field(             'awc_cpt', // id             'custom post types', // title             array( $this, 'awc_cpt_callback' ), // callback             'awc-redirect-admin', // page             'awc_redirect_setting_section' // section         );     }      public function awc_redirect_sanitize($input) {         $sanitary_values = array();         if ( isset( $input['awc_cpt'] ) ) {             $sanitary_values['awc_cpt'] = $input['awc_cpt'];         }          return $sanitary_values;     }      public function awc_redirect_section_info() {      }      public function awc_cpt_callback() {          ?> <select name="awc_redirect_option_name[awc_cpt]" id="awc_cpt" multiple>             <?php echo $this->awc_get_post_types(); ?>         </select> <?php     }      // post types archived => true     public function awc_get_post_types(){          $post_types = $this->awc_list_post_types();         $op = '';          foreach ( $post_types  $post_type ) {             $op .= $this->awc_get_post_details( $post_type );         }         return $op;     }      public function awc_list_post_types(){         $args = array(             'public'   => true,             '_builtin' => false         );          $output = 'names'; // names or objects, note names default         $operator = 'and'; // 'and' or 'or'         $op = '';          $post_types = get_post_types( $args, $output, $operator );         return $post_types;     }      // loop through , spit out <select>. if set, mark selected.     public function awc_get_post_details( $post_type ){         $op = get_post_type_object( $post_type );         if( !$post_type || false === $op->has_archive ){             return;         }          $name = $op->name;         $label = $op->label;         $archive_link = get_post_type_archive_link( $post_type );          // had following because array_push doesn't work on empty variable...          // ...so set first, push rest.         if( empty( $this->non_archived_posts ) ){             $this->non_archived_posts[] = $name;         } else{             array_push( $this->non_archived_posts, $name );         }          $selected = (isset( $this->awc_redirect_options['awc_cpt'] ) && $this->awc_redirect_options['awc_cpt'] === $name ) ? 'selected' : '';          $select_options = '                 <option value="' . $name . '" ' . $selected . '>' . $label . '</option>         ';          return $select_options;     }      public function awc_template_redirect(){         $post_types = $this->awc_list_post_types();         foreach( $post_types $cpt ){             if ( is_singular( $cpt ) && get_post_type_object( $cpt )->has_archive ) {                 $redirectlink = get_post_type_archive_link( $cpt );                 wp_redirect( $redirectlink, 302 );                 exit;             }         }     }  }  $awc_redirect = new awc_redirect(); 


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -