如何从选项页面获取数据

时间:2018-02-20 作者:Ibrahim Al Khalil

https://codex.wordpress.org/index.php?title=Creating_Options_Pages&oldid=97268

此页面显示如何创建选项页面。

        // theme-settings.php

        <?php
    add_action(\'admin_menu\', \'talimiboard_register_menu_and_page\');

    function talimiboard_register_menu_and_page()
    {
        add_theme_page(\'Talimiboard theme settings\', \'Theme Settings\', \'administrator\', \'talimiboard_theme_settings\', \'talimiboard_theme_settings\');
        add_action(\'admin_init\', \'talimiboard_register_theme_settings\');
    }


    function talimiboard_register_theme_settings()
    {
        register_setting(\'talimiboard_theme_settings\', \'talimi_mapapi\');
        register_setting(\'talimiboard_theme_settings\', \'talimi_smtphost\');
        register_setting(\'talimiboard_theme_settings\', \'talimi_smtpport\');
        register_setting(\'talimiboard_theme_settings\', \'talimi_smtpuser\');
        register_setting(\'talimiboard_theme_settings\', \'talimi_smtppass\');
        register_setting(\'talimiboard_theme_settings\', \'talimi_smtpfrom\');
        register_setting(\'talimiboard_theme_settings\', \'talimi_parallax\');
    }


function talimiboard_theme_settings()
{ ?>

    <style>
        th {
            text-align: left;
        }

        .table-wrap {
            margin-top: 35px;
        }
    </style>

    <div class="wrap">
        <h2 style="text-align: center;">Theme Settings</h2>
        <form method="post" action="options.php">
            <?php settings_fields(\'talimiboard_theme_settings\'); ?>
            <div class="table-wrap">
                <h3>Appearances</h3>
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row"><label
                                    for="talimi_parallax">Homepage Parallax Effect</label></th>
                        <td><input type="checkbox" id="talimi_parallax" name="talimi_parallax"
                                   value="1" <?php echo checked(\'1\', get_option(\'talimi_parallax\')); ?>/></td>
                    </tr>
                </table>
            </div>

            <div class="table-wrap">
                <h3>API Keys</h3>
                <table>
                    <tr valign="top">
                        <th scope="row">Google Map API Key</th>
                        <td><input type="text" name="talimi_mapapi" value="<?php echo get_option(\'talimi_mapapi\'); ?>"/>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="table-wrap">
                <h3>SMTP Settings</h3>
                <table>
                    <tr valign="top">
                        <th scope="row">SMTP Host</th>
                        <td>
                            <input type="text" name="talimi_smtphost"
                                   value="<?php echo get_option(\'talimi_smtphost\'); ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">SMTP Port</th>
                        <td>
                            <input type="text" name="talimi_smtpport"
                                   value="<?php echo get_option(\'talimi_smtpport\', 587); ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <th>From</th>
                        <td>
                            <input type="text" name="talimi_smtpfrom"
                                   value="<?php echo get_option(\'talimi_smtpfrom\'); ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <th>SMTP Account</th>
                        <td><input type="text" name="talimi_smtpuser"
                                   value="<?php echo get_option(\'talimi_smtpuser\'); ?>"/></td>
                    </tr>
                    <tr>
                        <th>Password</th>
                        <td><input type="password" name="talimi_smtppass"
                                   value="<?php echo get_option(\'talimi_smtppass\'); ?>"/></td>
                    </tr>
                </table>
            </div>
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e(\'Save Changes\') ?>"/>
            </p>
        </form>
    </div>
<?php } ?>

<?php 
// functions.php

require_once(get_template_directory_ur() . \'/page-templates/theme-settings.php\');

$google_map_api_key = get_option(\'talimi_mapapi\');

 ?>
然后echo$google\\u map\\u api\\u键什么也不显示。。

2 个回复
最合适的回答,由SO网友:DHL17 整理而成

我重复了这个$google_map_api_key = get_option(\'talimi_mapapi\'); 而且工作正常。

试一试functions.php 添加require_once get_template_directory() . \'/page-templates/theme-settings.php\';

或添加wp-load.php 在您的文件中。

如果使用多站点,请尝试<?php get_site_option( $option, $default , $use_cache ) ?> 参考https://codex.wordpress.org/Function_Reference/get_site_option

SO网友:Peter HvD

您使用get_option() 功能(参见codex) 输入您给字段使用的名称add_settings_field()

例如:echo get_option( \'eg_setting_name\' );

结束

相关推荐