我正在学习如何在WordPress中制作自定义仪表板页面和设置。我所遵循的教程非常清楚……但是有一件小事我不明白。
在我的 functions .php的demo_footer_message_display()
函数底部附近有一行代码,表示option['message']
。我不明白"消息"是在哪里定义的。如果它说option[' footer_message '],那就很有意义了,因为' footer_message '是我正在处理的字段的id。
代码工作正常,但我只是不明白为什么?在哪里' message '被定义为选项数组的索引。有人能给我解释一下吗?
p。如果它不是很明显,我的设置页面只有一个输入,你可以在主题的页脚输入一些文本。
显然也<?php /* --------------------------------------------* * Menus /*---------------------------------------------*/ // Adds the 'DEMO THEME OPTIONS' to the 'Settings' menu in WordPress function demo_add_options_page(){ add_options_page( 'Lindsay Demo Theme Options', //browser title 'Lindsay Demo Theme Options', // menu text 'manage_options', // required capability of users to access this menu 'lindsay-demo-theme-options', // slug 'demo_theme_option_display' // name of function used to display content ); } add_action('admin_menu', 'demo_add_options_page'); /* --------------------------------------------* * Sections, Settings, and Fields /*---------------------------------------------*/ // Registers a new settings field on the 'Demo Theme Options' page function demo_initialize_theme_options(){ //section to be rendered on the new options page add_settings_section( 'footer_section', //id 'Footer Options', //title on screen 'demo_footer_options_display', //callback 'lindsay-demo-theme-options'//ID of page ); //define the settings field add_settings_field( 'footer_message', //ID of field 'Theme Footer Message', //label 'demo_footer_message_display', //callback 'lindsay-demo-theme-options', // page 'footer_section' // the section to add to ); //Register the 'footer_message' setting with the 'General' section register_setting( 'footer_section', //name of the group of settings 'footer_options' //name of option ); } //end demo_initialize_theme_options add_action('admin_init', 'demo_initialize_theme_options'); /* --------------------------------------------* * Callbacks /*---------------------------------------------*/ function demo_theme_option_display(){ ?> <div class="wrap"> <h2 >Demo Theme Options</h2> <form method="post" action="options.php"> <?php // render the settings for the settings section identified as 'Footer section' settings_fields('footer_section'); //render all of the settings for 'demo-theme-options' sections do_settings_sections('lindsay-demo-theme-options'); // add the submit button to serialize options submit_button(); ?> </form> </div> <?php } function demo_footer_message_display(){ $options = (array)get_option('footer_options'); $message = $options['message']; echo '<input type="text" name="footer_options[message]" id="footer_options_message" value="'.$message.'"/>'; } function demo_footer_options_display(){ echo 'These options are designed to help you control whats dislayed in your footer'; }
footer。php
<div id="footer" class="col-md-12">
<?php wp_footer() ?>
<div class="site-info">
<a href="http://wordpress.org/" title="A Semantic Personal Publishing Platform" rel="generator">Proudly Powered by WordPress</a>
<span class="sep"> | </span>
<?php $options = (array) get_option('footer_options');?>
<?php $message = $options['message'];?>
<span id="footer-message"><?php echo $message; ?></span>
</div>
</div>
如果你看一下get_option方法之前的单词(array),它表明他正在将返回转换为数组(这篇文章说它是一个序列化的字符串)。settings api还指出get_option的返回类型是"mixed"。我猜,表单是张贴文本字段,呈现在选项页面上保存一切从文章到数据库中,当你检索它,它更容易将其转换为一个数组来检索文章的"消息"部分。试着取消它的类型转换,并使用"var_dump"来查看它返回的内容,看看你是否能得到一个比我的更好的答案:)
$options = get_option('footer_options');
var_dump($options)
http://codex.wordpress.org/Settings_API