我用于创建管理选项的代码不起作用

时间:2015-01-12 作者:drake035

我使用了中提供的代码this tutorial 要在仪表板中创建选项菜单,但与预期相反,我在仪表板中没有看到任何新菜单:

<?php
add_action(\'admin_menu\', \'add_global_custom_options\');
function add_global_custom_options()
{
    add_options_page(\'Global Custom Options\', \'Global Custom Options\', \'manage_options\', \'functions\',\'global_custom_options\');
}
function global_custom_options()
{
?>
    <div class="wrap">
        <h2>Global Custom Options</h2>
        <form method="post" action="options.php">
            <?php wp_nonce_field(\'update-options\') ?>
            <p><strong>Twitter ID:</strong><br />
                <input type="text" name="twitterid" size="45" value="<?php echo get_option(\'twitterid\'); ?>" />
            </p>
            <p><input type="submit" name="Submit" value="Store Options" /></p>
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="page_options" value="twitterid" />
        </form>
    </div>
<?php
}
我做错了什么?我在我的子主题中添加了此代码functions.php.

2 个回复
SO网友:M Ali Salim

确保您的函数、Slug和回调函数是唯一的(意味着不同于任何其他插件或默认WordPress)。还可以使用设置API保存选项http://codex.wordpress.org/Function_Reference/register_setting

<?php
add_action(\'admin_menu\', \'add_unique_custom_options\');
function add_unique_custom_options()
{
    add_options_page(\'Global Custom Options\', \'Global Custom Options\', \'manage_options\', \'unique-functions\',\'unique_custom_options\');
}
function unique_custom_options()
{
?>
    <div class="wrap">
        <h2>Global Custom Options</h2>
        <form method="post" action="options.php">
            <?php wp_nonce_field(\'update-options\') ?>
            <p><strong>Twitter ID:</strong><br />
                <input type="text" name="twitterid" size="45" value="<?php echo get_option(\'twitterid\'); ?>" />
            </p>
            <p><input type="submit" name="Submit" value="Store Options" /></p>
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="page_options" value="twitterid" />
        </form>
    </div>
<?php
}

SO网友:TheDeadMedic

要添加顶级菜单页,需要使用add_menu_page 而不是add_options_page (将子菜单项添加到“Settings”(设置)菜单)。

add_menu_page(
    $page_title,
    $menu_title,
    $capability,
    $menu_slug,
    $callback_function,
    $icon_url,
    $menu_position
);

http://codex.wordpress.org/Function_Reference/add_menu_page

结束