doukuiqian5345 2014-08-24 00:22
浏览 24
已采纳

WordPress创建设置页面

I'm trying to learn how to make custom dashboard pages and settings in WordPress. The tutorial I followed was pretty clear...but there's one very small thing that doesn't make sense to me.

There is a line of code near the bottom of my funcions.php in the demo_footer_message_display() function that says option['message']. I don’t understand where ‘message’ is ever defined. If it said option[‘footer_message’] that would make plenty of sense because ‘footer_message’ is the id of the field I’m dealing with.

The code works fine but I just don’t understand why? Where is ‘message’ ever defined as an index of the options array. Can anyone explain this to me?

PS. in case it's not obvious my settings page just has an input where you can enter some text in the footer of the theme.

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条回答 默认 最新

  • doucheng5209 2014-08-24 00:50
    关注

    If you look at the words (array) before the method get_option, it shows that he is casting the return as an array (this post says it's a serialised string).The settings api also says that the return type for get_option is "mixed". I would guess that the form is posting the text field that is rendered on the options page saves everything from the post into the database and when you retrieve it it's easier to cast it to an array to retrieve the "message" portion of the post. Try uncasting it and using "var_dump" to see what it returns and see if you can come up with a better answer than mine :)

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

    http://codex.wordpress.org/Settings_API

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退