Everyone!
I am new to WP and trying to build a plugin. I have the following codes working properly:
add_filter('the_content', 'say_hello');
function say_hello($content){
if(is_single() || is_page()){
print $content." Thank you for reading!";
} else {
print $content;
}
}
But the following codes don’t seem to work:
add_action('init', 'prepare_head');
function prepare_head(){
if(is_single() || is_page()){
// include javascripts
wp_enqueue_script("jquery");
//some more js goes here (registered and enqueued)...
//include css
$styleSrc = plugins_url("css/style.css", __FILE__);
wp_register_style("say_hello_style", $styleSrc);
wp_enqueue_style("say_hello_style");
}
}
Am I doing it wrong? Help is much appreciated! Thanks!
init
is too early for conditional tags, usetemplate_redirect
instead. have a look at the action reference to see the order they’re executed.The Conditional Tags that WordPress offers can only be used on the
template_redirect
hook or later.init
is far too soon. Thethe_content
filter occurs later on and can use the Conditional Tags. As you are new to WordPress plugins, take a look at Rarst’s graphical explanation of how the WordPress core loads. It will help with these types of issues.