0

I am trying to load my style sheets in my plugin but they are not being called, the javascript files however, are being loaded.

<?php
/**
Plugin Name: 
Plugin URI: 
Description: 
Version:
Author: 
Author URI: 
**/

define("MPF_PATH", ABSPATH . 'wp-content/plugins/' . basename(dirname(__FILE__)).'/' );
define("MPF_URL", trailingslashit(get_option('siteurl')) . 'wp-content/plugins/' . basename(dirname(__FILE__)) . '/' ); 


class mpf_plugin{

function crf_user_registration(){
    add_action('plugins_loaded', array(&$this, "load_my_scripts"));
    add_action('admin_menu', array(&$this,'add_crf_menu_items'));

    if(!is_admin()){
        add_shortcode('crf_registration', array(&$this, "do_shortcode"));
    } else {
        //do something else here

    }
}

function load_my_scripts(){
    if(is_admin()){
        wp_register_script('crf_script', MPF_URL . 'includes/js/crf_script.js');
        wp_register_style('crf_style', MPF_URL . 'includes/css/crf_style.css');
        wp_enqueue_style('crf-style');

        wp_enqueue_script('jquery');
        wp_enqueue_script('crf_script');

        wp_localize_script( 'crf_script', 'MyAjax', array( 'MPF_PATH' => MPF_PATH, 'MPF_URL' => MPF_URL,'ajaxurl' => MPF_URL . 'includes/save-layout.php', 'loadPageLayoutURL' => MPF_URL . 'includes/get-layout.php', 'optionsPageURL' => MPF_URL . 'includes/options.php' ));
    }
}


function do_shortcode(){

    include('crf_form.php');
}


function options_page() {
    // HTML code goes here, left out for brevity
}

function custom_dropdown_page(){
    // HTML code goes here, left out for brevity
}

function add_crf_menu_items() {
    add_menu_page(
            'My Plugin Form', 
            'MPF', 
            'manage_options', 
            'mpf-options', 
            array(&$this,'options_page')
        );

    add_submenu_page(
            'mpf-options', 
            'MPF Options', 
            'MPF Options', 
            'manage_options', 
            'custom-mpf-options', 
            array(&$this,'custom_dropdown_page')
        );
}

}

new mpf_plugin();

The paths are all correct. Any ideas as to what is going on?

1 Answer 1

1

I'll guess it's because plugins_loaded is too early to enqueue styles. The wp_enqueue_scripts action is the appropriate action for enqueueing scripts and styles.

5
  • Thanks Milo. What would you think is the right action to hook into? And if plugins_loaded is too early then why does the javascript file load?
    – Todd
    Jun 27, 2012 at 18:34
  • without looking into source, I'm not sure why the two would behave differently, but as I mention in my answer wp_enqueue_scripts is the action you should hook. see the example with wp_enqueue_style in codex.
    – Milo
    Jun 27, 2012 at 19:50
  • Thanks again Milo. I had read your first response wrong. Strange, but the style sheets still don't load with this hook. I don't get it.
    – Todd
    Jun 27, 2012 at 21:10
  • Did you see this example Todd? codex.wordpress.org/Function_Reference/… - looks like 'admin_init' is the action to use for registering. Jun 27, 2012 at 21:56
  • @MereDevelopment yes, I have seen that and tried that as well but still to no avail. It has to be something else that is going on as I have the scripts loading just fine using plugins_loaded and admin_init in my other plugins. Thanks for chiming in Mere Dev.
    – Todd
    Jun 27, 2012 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.