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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值