【问题标题】:WordPress creating settings pageWordPress 创建设置页面
【发布时间】:2014-08-24 08:50:20
【问题描述】:

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

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

代码运行良好,但我就是不明白为什么? “消息”在哪里被定义为选项数组的索引。谁能给我解释一下?

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>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果你查看get_option方法前面的单词(array),它表明他正在将返回转换为数组(这篇文章说it's a serialised string)。设置api还说get_option的返回类型是“混”。我猜该表单正在发布选项页面上呈现的文本字段,将帖子中的所有内容保存到数据库中,当您检索它时,更容易将其转换为数组以检索帖子的“消息”部分。尝试取消它并使用“var_dump”来查看它返回的内容,看看你是否能想出比我更好的答案:)

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

    http://codex.wordpress.org/Settings_API

    【讨论】:

    • 谢谢,好建议。 var_dump($options); 的结果是: array(1) { ["message"]=> string(5) "tests" } 在我输入单词 tests' 到我的输入字段之后。我仍然不确定“消息”来自何处……我猜它是 WordPress 的 get_options() 函数的一部分?我只是没有真正看到任何表明这一点的文档。
    • 尝试访问您的 wordpress 后端中的选项页面,并在文本字段所在的表单上使用检查元素。如果与页脚消息对应的输入字段有一个包含单词 message 的数组,那么您将知道它是否已设置。如果它不存在,那么它可能意味着它是表单创建的发布操作的“消息”......希望这是有道理的。
    • 嗯,好的。所以我替换了我的代码中所有说“消息”的地方,并将它们更改为测试,我的代码仍然有效。所以我看到这不是由 wordpress 定义的,我认为当你最初制作 $message = $options['message'] 语句时......你将'message'作为选项数组中的键引入...... .then 该键的值由输入的值定义。
    • 感谢您帮助我查看此内容。
    • 感谢您实际尝试了解它的工作原理 :)
    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2014-07-23
    • 2016-08-16
    • 2021-01-30
    • 1970-01-01
    相关资源
    最近更新 更多