Notice that the wp_enqueue_style is not being called correctly!

I get this error due to one of the plugins which I rely on.

Notice: wp_enqueue_style was called incorrectly. Scripts and styles
should not be registered or enqueued until the wp_enqueue_scripts,
admin_enqueue_scripts, or init hooks.

Read More

What does this error mean and how do you fix it?
Mind due that this is not a critical error. It only appears when I put WP into debug mode.

The full plugin-code is below.

It’s one heck of a plug in that makes it a breeze to deal with the highly complicated and hierarchical cats. I’m posting it not just for the sake of debugging where the bloddy error is but also to hope that it helps someone.

<?php /*

**************************************************************************

Plugin Name:  Category Collapse
Description:  Makes children categories hidden on the Write screens until an icon is clicked.
Version:      2009.02.12
Author:       Viper007Bond
Author URI:   http://www.viper007bond.com/

**************************************************************************/

class CategoryCollapse {

    // Plugin initialization
    function CategoryCollapse() {
        if ( !is_admin() ) return;

        wp_enqueue_style( 'category-collapse', plugins_url('/category-collapse/category-collapse.css'), array(), '2009.02.12', 'screen' );
        add_action( 'admin_head', array(&$this, 'edit_form_advanced') );
    }


    // Output the Javascript (it's not in an external file due to the dynamic image URLs)
    function edit_form_advanced() { ?>
<script type="text/javascript">
/* <![CDATA[ */
    jQuery(document).ready(function($){
        $("#categorychecklist li").addClass("catcolpadding");
        $("#categorychecklist ul.children").hide().parent("li").removeClass("catcolpadding").addClass("cathaschildren catcollapsed").prepend('<img class="catcoltoggler" src="<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>" alt="+" title="Show Children" />');
        $("#categorychecklist .catcoltoggler").click(function(){
            var parent = $(this).parent("li.cathaschildren");
            if ( parent.hasClass("catcollapsed") ) {
                $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/minus.gif'); ?>", alt: "-", title: "Hide Children" });
                parent.removeClass("catcollapsed").addClass("catuncollapsed").children("ul.children").slideDown();
            } else {
                $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>", alt: "+", title: "Show Children" });
                parent.removeClass("catuncollapsed").addClass("catcollapsed").children("ul.children").slideUp();
            }
        });
        $(".cat-checklist li").addClass("catcolpadding");
        $(".cat-checklist ul.children").hide().parent("li").removeClass("catcolpadding").addClass("cathaschildren catcollapsed").prepend('<img class="catcoltoggler" src="<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>" alt="+" title="Show Children" />');
        $(".cat-checklist .catcoltoggler").click(function(){
            var parent = $(this).parent("li.cathaschildren");
            if ( parent.hasClass("catcollapsed") ) {
                $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/minus.gif'); ?>", alt: "-", title: "Hide Children" });
                parent.removeClass("catcollapsed").addClass("catuncollapsed").children("ul.children").slideDown();
            } else {
                $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>", alt: "+", title: "Show Children" });
                parent.removeClass("catuncollapsed").addClass("catcollapsed").children("ul.children").slideUp();
            }
        });
    });
/* ]]> */
</script>
<?php
    }
}

// Start this plugin once all other plugins are fully loaded
add_action( 'plugins_loaded', create_function( '', 'global $CategoryCollapse; $CategoryCollapse = new CategoryCollapse();' ) );

?>

Related posts

Leave a Reply

2 comments

  1. The problem is that the wp_enqueue_style() call is inside of the category_collapse() member function of the CategoryCollapse() class, and the CategoryCollapse() class is being instantiated by a callback hooked into the plugins_loaded action hook.

    That means that the wp_enqueue_style() function is attempting to execute at the plugins_loaded hook, which fires before init, wp_enqueue_scripts, and admin_enqueue_scripts.

    To fix, replace this:

    wp_enqueue_style( 'category-collapse', plugins_url('/category-collapse/category-collapse.css'), array(), '2009.02.12', 'screen' );
    

    …with this:

    function wpse49339_enqueue_styles() {
        wp_enqueue_style( 'category-collapse', plugins_url('/category-collapse/category-collapse.css'), array(), '2009.02.12', 'screen' );
    }
    add_action( 'wp_enqueue_scripts', 'wpse49339_enqueue_styles' );
    

    That way, the wp_enqueue_style() call will be hooked into wp_enqueue_scripts instead of firing directly at plugins_loaded.

  2. //here is the working and bugfree code

    <?php /*
    
    **************************************************************************
    
    Plugin Name:  Category Collapse
    Description:  Makes children categories hidden on the Write screens until an icon is clicked.
    Version:      2009.02.12
    Author:       Viper007Bond
    Author URI:   http://www.viper007bond.com/
    
    **************************************************************************/
    
    class CategoryCollapse {
    
        // Plugin initialization
        function CategoryCollapse() {
            if ( !is_admin() ) return;
            //begin modifitication  
            //per bennet's suggestion on http://wordpress.stackexchange.com/questions/49339/notice-that-the-wp-enqueue-style-is-not-being-called-correctly/49344#49344      
            //the following org code is commented out
            //wp_enqueue_style( 'category-collapse', plugins_url('/category-collapse/category-collapse.css'), array(), '2009.02.12', 'screen' );
            //and replaced with this;
            function wpse49339_enqueue_styles() {
                wp_enqueue_style( 'category-collapse', plugins_url('/category-collapse/category-collapse.css'), array(), '2009.02.12', 'screen' );
            }
            add_action( 'wp_enqueue_scripts', 'wpse49339_enqueue_styles' );
            //end modification  
            add_action( 'admin_head', array(&$this, 'edit_form_advanced') );
        }
    
    
        // Output the Javascript (it's not in an external file due to the dynamic image URLs)
        function edit_form_advanced() { ?>
    <script type="text/javascript">
    /* <![CDATA[ */
        jQuery(document).ready(function($){
            $("#categorychecklist li").addClass("catcolpadding");
            $("#categorychecklist ul.children").hide().parent("li").removeClass("catcolpadding").addClass("cathaschildren catcollapsed").prepend('<img class="catcoltoggler" src="<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>" alt="+" title="Show Children" />');
            $("#categorychecklist .catcoltoggler").click(function(){
                var parent = $(this).parent("li.cathaschildren");
                if ( parent.hasClass("catcollapsed") ) {
                    $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/minus.gif'); ?>", alt: "-", title: "Hide Children" });
                    parent.removeClass("catcollapsed").addClass("catuncollapsed").children("ul.children").slideDown();
                } else {
                    $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>", alt: "+", title: "Show Children" });
                    parent.removeClass("catuncollapsed").addClass("catcollapsed").children("ul.children").slideUp();
                }
            });
            $(".cat-checklist li").addClass("catcolpadding");
            $(".cat-checklist ul.children").hide().parent("li").removeClass("catcolpadding").addClass("cathaschildren catcollapsed").prepend('<img class="catcoltoggler" src="<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>" alt="+" title="Show Children" />');
            $(".cat-checklist .catcoltoggler").click(function(){
                var parent = $(this).parent("li.cathaschildren");
                if ( parent.hasClass("catcollapsed") ) {
                    $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/minus.gif'); ?>", alt: "-", title: "Hide Children" });
                    parent.removeClass("catcollapsed").addClass("catuncollapsed").children("ul.children").slideDown();
                } else {
                    $(this).attr({ src: "<?php echo plugins_url('/category-collapse/images/plus.gif'); ?>", alt: "+", title: "Show Children" });
                    parent.removeClass("catuncollapsed").addClass("catcollapsed").children("ul.children").slideUp();
                }
            });
        });
    /* ]]> */
    </script>
    <?php
        }
    }
    
    // Start this plugin once all other plugins are fully loaded
    add_action( 'plugins_loaded', create_function( '', 'global $CategoryCollapse; $CategoryCollapse = new CategoryCollapse();' ) );
    
    ?>