WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
        //some more js goes here (registered and enqueued)... 

        //include css
        $styleSrc = plugins_url("css/style.css", __FILE__);
        wp_register_style("say_hello_style", $styleSrc);

Am I doing it wrong? Help is much appreciated! Thanks!

share|improve this question
up vote 2 down vote accepted

init is too early for conditional tags, use template_redirect instead. have a look at the action reference to see the order they're executed.

share|improve this answer
Thanks! Worked. Since I am new to WP, could you recommend any good book (or link) that talks in details about ALL hooks? – abhisek Sep 13 '11 at 3:47
@tollmanz, thanks :-) – abhisek Sep 13 '11 at 3:54

The Conditional Tags that WordPress offers can only be used on the template_redirect hook or later. init is far too soon. The the_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.

share|improve this answer
That is very very helpful! Thanks a ton! – abhisek Sep 13 '11 at 3:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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