4

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.

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();' ) );

?>
2
  • thanks. Tried it but did not work. the new error is Notice: wp_register_style was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or init hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.) in ... on line 3587 Commented Apr 17, 2012 at 20:28
  • Would you mind posting the plugin's link? I cannot find it neither in the repository nor in Viper007Bond page...
    – brasofilo
    Commented Apr 17, 2012 at 23:14

2 Answers 2

8

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.

1
  • It worked Chip. Thank you. I'm going to paste the working code as an answer below for somebody to simply copy and go. Commented Apr 17, 2012 at 21:33
0

//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();' ) );

?>

Your Answer

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

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