WordPress创建设置页面

时间:2014-08-24 00:22:40

标签: php wordpress

我正在尝试学习如何在WordPress中制作自定义仪表板页面和设置。我所遵循的教程非常清楚......但是有一件非常小的事情对我来说没有意义。

demo_footer_message_display()函数中 funcions.php 底部附近有一行代码option['message']。我不明白“消息”的定义。如果它说选项['footer_message']会很有意义,因为'footer_message'是我正在处理的字段的id。

代码工作正常,但我不明白为什么? 'message'在哪里被定义为options数组的索引。任何人都可以向我解释这个吗?

PS。如果不明显我的设置页面只有一个输入,您可以在主题的页脚中输入一些文本。

的functions.php

<?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>

1 个答案:

答案 0 :(得分:1)

如果你在方法get_option之前查看单词(数组),它会显示他将返回作为数组进行转换(这篇文章说it's a serialised string)。设置api也表示get_option的返回类型是&#34;混合&#34;。我猜这个表单是发布在选项页面上呈现的文本字段,保存从帖子到数据库的所有内容,当你检索它时,它更容易将它转换为数组来检索&#34;消息&#34;部分帖子。尝试取消它并使用&#34; var_dump&#34;看看它返回的内容,看看你能否找到比我更好的答案:)

$options = get_option('footer_options');
var_dump($options)

http://codex.wordpress.org/Settings_API