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();' ) );
?>
The problem is that the
wp_enqueue_style()
call is inside of thecategory_collapse()
member function of theCategoryCollapse()
class, and theCategoryCollapse()
class is being instantiated by a callback hooked into theplugins_loaded
action hook.That means that the
wp_enqueue_style()
function is attempting to execute at theplugins_loaded
hook, which fires beforeinit
,wp_enqueue_scripts
, andadmin_enqueue_scripts
.To fix, replace this:
…with this:
That way, the
wp_enqueue_style()
call will be hooked intowp_enqueue_scripts
instead of firing directly atplugins_loaded
.//here is the working and bugfree code