Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
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 '12 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 '12 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 '12 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. –  Mere Development Jun 27 '12 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 '12 at 22:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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