WordPress设置字段:保存多个<select>选项

时间:2017-09-11 21:23:38

标签: php wordpress

在WordPress选项页面中,我有一个设置字段,其中包含属性为multiple的HTML选择。自定义帖子类型动态填充选择的选项,一切都很好。我可以将一个值保存到数组中,但不能更多。这是当前的var_dump: array(1){[&#34; awc_cpt&#34;] =&gt; string(12)&#34; board_member&#34; } 理想情况下,我希望返回的数组: 阵列(     &#34; board_member&#34; =&GT; &#34; board_member&#34 ;,     &#34; another_cpt&#34; =&GT; &#34; another_cpt&#34 ;,     //等等,根据需要提供尽可能多的自定义帖子类型 ) 我是PHP的初学者,所以我可能会缺少一些基础知识。 问题:如何将多个选定选项保存到此WordPress设置的数组中? &LT; PHP AWC_Redirect类{     private $ awc_redirect_options;     public $ non_archived_posts = array();     public function __construct(){         if(is_admin()){             add_action(&#39; admin_menu&#39;,array($ this,&#39; awc_redirect_add_plugin_page&#39;));             add_action(&#39; admin_init&#39;,数组($ this,&#39; awc_redirect_page_init&#39;));         }         add_action(&#39; template_redirect&#39;,array($ this,&#39; AWC_template_redirect&#39;));     }     公共函数awc_redirect_add_plugin_page(){         add_options_page(             &#39; AWC重定向&#39;,// page_title             &#39; AWC重定向&#39;,// menu_title             &#39; manage_options&#39;,//功能             &#39; awc-redirect&#39;,// menu_slug             数组($ this,&#39; awc_redirect_create_admin_page&#39;)//函数         );     }     public function awc_redirect_create_admin_page(){         $ this-&gt; awc_redirect_options = get_option(&#39; awc_redirect_option_name&#39;); ?&GT;         &lt; div class =&#34; wrap&#34;&gt;             &lt; h2&gt; AWC重定向&lt; / h2&gt;             &LT p为H.;&LT; / p为H.             &lt;?php settings_errors(); ?&GT;             &lt; form method =&#34; post&#34;行动=&#34; options.php&#34;&GT;                 &LT; PHP                     settings_fields(&#39; awc_redirect_option_group&#39;);                     do_settings_sections(&#39; awc-redirect-admin&#39;);                     submit_button();                 ?&GT;             &LT; /形式&GT;         &LT; / DIV&GT;         &lt;?php var_dump(get_option(&#39; awc_redirect_option_name&#39;)); ?&GT;     &lt;?php}     public function awc_redirect_page_init(){         register_setting(             &#39; awc_redirect_option_group&#39;,// option_group             &#39; awc_redirect_option_name&#39;,// option_name             数组($ this,&#39; awc_redirect_sanitize&#39;)// sanitize_callback         );         add_settings_section(             &#39; awc_redirect_setting_section&#39;,// id             &#39;设置&#39;,//标题             数组($ this,&#39; awc_redirect_section_info&#39;),//回调             &#39; AWC重定向管理员&#39; //页面         );         add_settings_field(             &#39; awc_cpt&#39;,// id             &#39;自定义帖子类型&#39;,//标题             数组($ this,&#39; awc_cpt_callback&#39;),//回调             &#39; awc-redirect-admin&#39;,//页面             &#39; awc_redirect_setting_section&#39; // 部分         );     }     public function awc_redirect_sanitize($ input){         $ sanitary_values = array();         if(isset($ input [&#39; awc_cpt&#39;])){             $ sanitary_values [&#39; awc_cpt&#39;] = $ input [&#39; awc_cpt&#39;];         }         返回$ sanitary_values;     }     public function awc_redirect_section_info(){     }     public function awc_cpt_callback(){         ?&GT; &lt; select name =&#34; awc_redirect_option_name [awc_cpt]&#34; ID =&#34; awc_cpt&#34;多个&GT;             &lt;?php echo $ this-&gt; AWC_get_post_types(); ?&GT;         &LT; /选择&GT; &LT; PHP     }     //获取所有已归档的帖子类型=&gt;真正     公共函数AWC_get_post_types(){         $ post_types = $ this-&gt; AWC_list_post_types();         $ op =&#39;&#39 ;;         foreach($ post_types as $ post_type){             $ op。= $ this-&gt; AWC_get_post_details($ post_type);         }         return $ op;     }     公共职能AWC_list_post_types(){         $ args = array(             &#39;公&#39; =&GT;真正,             &#39; _builtin&#39; =&GT;假         );         $ output =&#39;名称&#39 ;; //名称或对象,注释名称是默认名称         $ operator =&#39;和&#39;; //&#39;和&#39;或者&#39;或&#39;         $ op =&#39;&#39 ;;         $ post_types = get_post_types($ args,$ output,$ operator);         return $ post_types;     }     //循环并吐出到&lt; select&gt;。如果已设置,则标记为已选中。     公共函数AWC_get_post_details($ post_type){         $ op = get_post_type_object($ post_type);         if(!$ post_type || false === $ op-&gt; has_archive){             返回;         }         $ name = $ op-&gt; name;         $ label = $ op-&gt; label;         $ archive_link = get_post_type_archive_link($ post_type);         //必须执行以下操作,因为array_push不能处理空变量...         // ...所以用第一个设置它,用其余的推动。         if(empty($ this-&gt; non_archived_posts)){             $ this-&gt; non_archived_posts [] = $ name;         } else {             array_push($ this-&gt; non_archived_posts,$ name);         }         $ selected =(isset($ this-&gt; awc_redirect_options [&#39; awc_cpt&#39;])&amp;&amp; $ this-&gt; awc_redirect_options [&#39; awc_cpt&#39;] === $ name)? &#39;选择&#39; :&#39;&#39 ;;         $ select_options =&#39;                 &lt; option value =&#34;&#39; 。 $ name。 &#39;&#34; &#39; 。 $ selected。 &#39;&GT;&#39; 。 $ label。 &#39;&LT; /选项&GT;         &#39 ;;         return $ select_options;     }     公共函数AWC_template_redirect(){         $ post_types = $ this-&gt; AWC_list_post_types();         foreach($ post_types为$ cpt){             if(is_singular($ cpt)&amp;&amp; get_post_type_object($ cpt) - &gt; has_archive){                 $ redirectLink = get_post_type_archive_link($ cpt);                 wp_redirect($ redirectLink,302);                 出口;             }         }     } } $ awc_redirect = new AWC_Redirect();

0 个答案:

没有答案