如何为这个简单的插件创建选项页面

时间:2013-12-30 作者:Hardeep Asrani

我知道如何编写WordPress插件,但我不知道如何为插件创建选项页。我读了很多教程,但我仍然不知道如何创建选项页。

因此,为了学习,我想创建一个选项页面,其中包含一个输入字段“Favicon URL”,该字段将更新FAVICON IMAGE URL 以下代码中的部分:

add_action(\'admin_head\', \'show_favicon\');
function show_favicon() {
echo \'<link href="FAVICON IMAGE URL" rel="icon" type="image/x-icon">\';
}

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

将下面的代码复制到函数中。php文件,

<?php add_menu_page(\'BAW Plugin Settings\', \'BAW Settings\', \'administrator\', __FILE__, \'baw_settings_page\', get_stylesheet_directory_uri(\'stylesheet_directory\')."/images/media-button-other.gif"); ?>

<?php
// create custom plugin settings menu
add_action(\'admin_menu\', \'baw_create_menu\');

function baw_create_menu() {

    //create new top-level menu
    add_menu_page(\'BAW Plugin Settings\', \'BAW Settings\', \'administrator\', __FILE__, \'baw_settings_page\',plugins_url(\'/images/icon.png\', __FILE__));

    //call register settings function
    add_action( \'admin_init\', \'register_mysettings\' );
}


function register_mysettings() {
    //register our settings
    register_setting( \'baw-settings-group\', \'new_option_name\' );
    register_setting( \'baw-settings-group\', \'some_other_option\' );
    register_setting( \'baw-settings-group\', \'option_etc\' );
}

function baw_settings_page() {
?>
<div class="wrap">
<h2>Your Plugin Name</h2>

<form method="post" action="options.php">
    <?php settings_fields( \'baw-settings-group\' ); ?>
    <?php do_settings_sections( \'baw-settings-group\' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">New Option Name</th>
        <td><input type="text" name="new_option_name" value="<?php echo get_option(\'new_option_name\'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Some Other Option</th>
        <td><input type="text" name="some_other_option" value="<?php echo get_option(\'some_other_option\'); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">Options, Etc.</th>
        <td><input type="text" name="option_etc" value="<?php echo get_option(\'option_etc\'); ?>" /></td>
        </tr>
    </table>

    <?php submit_button(); ?>

</form>
</div>
<?php } ?>
您可以在此处找到更多详细信息:http://codex.wordpress.org/Creating_Options_Pages

SO网友:Przemysław Suszek

创建菜单:

function ow_plugin_menu(){
    add_menu_page(\'Name\', \'Name\', \'administrator\', \'ow_admin\', \'ow_display_admin_settings\', plugins_url( \'icon.ico\' ));
}
add_action(\'admin_menu\', \'ow_plugin_menu\');
然后创建一个名为ow_display_admin_settings

结束