0
votes

I'm building a WordPress plugin, but I'm having trouble loading the css on the plugin's options page in the admin menu. I consulted the codex at http://codex.wordpress.org/Function_Reference/wp_enqueue_style

And here's what I got (I simplified the code by taking out all the parts irrelevant to my question and changing the plugin name):

<?php

if (!class_exists('My_Plugin')){
    class My_Plugin{

        public function __construct(){

            //add admin page
            add_action('admin_menu', array($this, 'my_plugin_menu'));
            add_action('admin_init', array($this, 'my_admin_init'));
        }

        //adds a page in the admin menu under posts
        public function my_admin(){

            //ensure user is admin
            if ( !current_user_can( 'manage_options' ) )  {
                wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
            }
            ?>
            <div class="wrap">
            <?php screen_icon(); ?>
            <h1>My Plugin</h1>
            </div>
            <?php
        }

        //admin initialization function
        public function my_admin_init(){
            //register admin page's css
            wp_register_style('my_style', plugins_url('my_style.css', __FILE__));
        }

        //add css files for admin menu()
        public function my_admin_enqueue_styles(){
            wp_enqueue_style('my_style');
        }

        //add menu page under posts
        public function my_plugin_menu(){
            $page = add_submenu_page('edit.php', 'My Plugin', 'My Plugin', 'manage_options', 'my-plugin', array($this, 'my_admin'));
            //add css
            add_action('admin_print_styles-'.$page, 'my_admin_enqueue_styles');
        }
    }

    //instantiate the class
    $mp = new My_Plugin();
}

In the my_style.css file I just stuck h1{color:red;} in order to test it out. The options page loads fine, but firebug isn't showing the css as being loaded at all. I'm stumped. What do I need to do?

1
am i right to say this has nothing to do with css? atleast the [css] tag - iConnor
I guess you're right. Thanks - Chris

1 Answers

0
votes

Figured it out... It was the line:

add_action('admin_print_styles-'.$page, 'my_admin_enqueue_styles');

Since I'm doing all this inside a class, I need to pass the second parameter as an array with $this

add_action('admin_print_styles-'.$page, array($this, 'my_admin_enqueue_styles'));

I did it with the other hooks, just missed this since it's not done that way in the codex. Silly me.