如何在插件设置页面中包含css?

时间:2013-07-29 作者:vero

我在WordPress仪表板菜单中创建了一个插件设置页面。我想包括该设置页面的css文件。我该怎么做?我正在插件中尝试以下内容,但插件设置页面中不包含css文件:

function my_enqueue_styles() {
    wp_enqueue_style( \'my_plugin\', plugins_url(\'my_plugin/plugin.css\') );
}
add_action( \'wp_enqueue_scripts\', \'my_enqueue_styles\' );

1 个回复
SO网友:Rajeev Vyas

参考文档Link Scripts Only on a Plugin Administration Screen

    add_action( \'admin_menu\', \'my_plugin_admin_menu\' );
 function my_plugin_admin_menu() {
        /* Add our plugin submenu and administration screen */
        $page_hook_suffix = add_submenu_page( \'edit.php\', // The parent page of this submenu
                                  __( \'My Plugin\', \'myPlugin\' ), // The submenu title
                                  __( \'My Plugin\', \'myPlugin\' ), // The screen title
                  \'manage_options\', // The capability required for access to this submenu
                  \'my_plugin-options\', // The slug to use in the URL of the screen
                                  \'my_plugin_manage_menu\' // The function to call to display the screen
                               );

        /*
          * Use the retrieved $page_hook_suffix to hook the function that links our script.
          * This hook invokes the function only on our plugin administration screen,
          * see: http://codex.wordpress.org/Administration_Menus#Page_Hook_Suffix
          */
        add_action(\'admin_print_scripts-\' . $page_hook_suffix, \'my_plugin_admin_scripts\');
    }

    function my_plugin_admin_scripts() {
        /* Link our already registered script to a page */
        wp_enqueue_script( \'my-plugin-script\' );
    }

    function my_plugin_manage_menu() {
        /* Display our administration screen */
    }

结束