css - 将样式表添加到我的选项页面

标签 css wordpress stylesheet

我的文件名是YPE-Options.php,文件根是panel/YPE-Options.php,我的样式表根是panel/css/bootstrap.min.css。我想在下面的代码中添加/加载或注册这个样式表

我该怎么做?

<?php

class YPE_Admin_Panel {

    public function __construct() {
        add_action('admin_menu', array($this, 'YPE_add_menu_page'));
    }

    public function YPE_add_menu_page() {
        //add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
        add_menu_page(
            'YALLANPE', 
            'YALLANPE', 
            'manage_options',
            'YPE_menu_page_options', 
            array($this,'YPE_display_admin_panel_options'), 
            '' 
        );
    }

    public function YPE_display_admin_panel_options() {
    ?>
        <h2>Yallanpe Blog Theme Settings</h2>
    <?php
    }

}

$YPE_Admin_Panel = new YPE_Admin_Panel();

最佳答案

函数add_menu_page()返回用于跟踪我们的自定义页面的 Hook 名称。它可以像这样使用:

$hook = add_menu_page( $arguments );
add_action( "admin_print_scripts-$hook", 'my_enqueue_callback' );

但是admin_print_scripts()建议仅打印内联脚本。要排队,有 admin_enqueue_scripts()它接收 $hook 作为参数。

完整示例:

class YPE_Admin_Panel 
{
    public $plugin_url;
    public $plugin_path;
    private $hook;

    public function __construct() 
    {
        $this->plugin_url    = plugins_url( '/', __FILE__ );
        $this->plugin_path   = plugin_dir_path( __FILE__ );
        add_action('admin_menu', array($this, 'menu_page'));
    }

    public function menu_page() 
    {
        $this->hook = add_menu_page(
            'YALLANPE', 
            'YALLANPE', 
            'manage_options',
            'YPE_menu_page_options', 
            array( $this,'display_panel' ), 
            '' 
        );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
    }

    public function display_panel() 
    {
        ?><h2>Yallanpe Blog Theme Settings</h2><?php
    }

    public function enqueue( $hook )
    {
        // Not our page, bail out
        if( $hook !== $this->hook )
            return;

        wp_enqueue_style( 'my-style', $this->plugin_url . 'css/custom.css' );
    }
}

$YPE_Admin_Panel = new YPE_Admin_Panel();

关于css - 将样式表添加到我的选项页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23716115/

相关文章:

WordPress:如何在自定义帖子类型中添加多个分类法

html - 使用 :host-context() with an adjacent sibling selector

html - 关于 float 和清除

css - 更改单个菜单项上的事件链接颜色

Html.Raw 改变整个页面的样式

html - 样式表中的 CSS 继承?

jquery - 为 Web 应用程序设计样式、颜色、感觉、CSS 原型(prototype)

html - 在 HTML 中删除成功验证时的输入移位

php - MySQL检查表的行数是否小于(N + 8)

css - 当您在 anchor 的所述页面上时,如何更改 anchor 内的图像