Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

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
1  
    
@tollmanz, thanks :-) –  abhisek Sep 13 '11 at 3:54
add comment

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
add comment

Your Answer

 
discard

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.