I get this error due to one of the plugins which I rely on.
Notice: wp_enqueue_style was called incorrectly. Scripts and stylesshould 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();\' ) );
?>
最合适的回答,由SO网友:Chip Bennett 整理而成
问题是wp_enqueue_style()
呼叫位于category_collapse()
的成员函数CategoryCollapse()
类,以及CategoryCollapse()
类正在由连接到plugins_loaded
行动挂钩。
这意味着wp_enqueue_style()
函数正在尝试在plugins_loaded
hook, which fires before init
, wp_enqueue_scripts
, and admin_enqueue_scripts
.
要修复此问题,请更换:
wp_enqueue_style( \'category-collapse\', plugins_url(\'/category-collapse/category-collapse.css\'), array(), \'2009.02.12\', \'screen\' );
。。。使用此选项:
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\' );
那样的话
wp_enqueue_style()
呼叫将连接到
wp_enqueue_scripts
而不是直接向
plugins_loaded
.
SO网友:Average Joe
//这是工作和无错误的代码
<?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();\' ) );
?>