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.

I need to run a function when a particular post or page is loaded. Is there any hook that lets me check whether a post is being displayed during page load ?

share|improve this question
    
public or admin side/facing? –  kaiser Oct 16 '12 at 10:48
    
For front end. Basically I want it to trigger only when a particular single custom post type is displayed. –  Poulomi Nag Oct 16 '12 at 10:52

3 Answers 3

up vote 4 down vote accepted

You can use the wp hook and check the global $wp_query object or any conditional.

function wpse69369_special_thingy()
{
    if (
        'special_cpt' === get_post_type()
        AND is_singular()
    )
        return print "Yo World!";

    return printf(
         '<p>Nothing to see here! Check the object!<br /></p><pre>%s</pre>'
        ,var_export( $GLOBALS['wp_query'], true )
    );
}
add_action( 'wp', 'wpse69369_special_thingy' );
share|improve this answer
    
can you please tell me when does 'wp' hook run? –  Poulomi Nag Oct 16 '12 at 11:07
    
A) It runs before after_setup_theme and setup_theme, so it's only accessible for plugins B) inside WP :: main(), which is called from within wp-settings.php. –  kaiser Oct 16 '12 at 11:19
    
Thanks kaiser. This works good. –  Poulomi Nag Oct 17 '12 at 6:12
    
@kaiser Doesn't the wp hook fire after the after_setup_theme hook and right before template_redirect therefore making wp accessible by themes as well as plugins? (just to clarify?) –  userabuser Oct 17 '12 at 6:18

Use template_redirect which is the action hook that fires before rendering the template;

add_action('template_redirect', 'hooker');
function hooker(){
    //I load just before selecting and rendering the template to screen
}
share|improve this answer
    
Thanks userabuser :) –  Poulomi Nag Oct 17 '12 at 6:08
    
@PoulomiNag No problem, glad you found your answer above. Though I think one small note is that wp runs after the after_theme_setup hook, so its not just accessible by plugins, making it safe to use in themes. –  userabuser Oct 17 '12 at 6:21
    
I just checked and yes ; wp runs after after_theme_setup. But I need it for my plugin. So wp as well as template_redirect both work fine for me. Wish I could accept two answers here! :) –  Poulomi Nag Oct 17 '12 at 7:33
    
That's ok, not necessary to accept both, just wanted to clarify the order in which they fire. Making sure I'm not going crazy you know. Good luck with your plugin... –  userabuser Oct 17 '12 at 7:42

I've quite often used the following to load in custom meta boxes on pages (rather than custom posts).

    add_action('admin_init','how_we_do_it_meta');

    function how_we_do_it_meta() {

        if ( $_SERVER['SCRIPT_NAME'] == '/wp-admin/post.php' ) {

            $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
            $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);

            if ($template_file == 'page-how-we-do-it.php') {

                add_meta_box('how_we_do_it_who-meta', 'Who we work with...', 'how_we_do_it_who', 'page', 'normal', 'high');

                add_action('save_post', 'save_how_we_do_it_meta');

            }
        }
    }
share|improve this answer
    
Thanks Darronz. But I need some hook to work at the front end during a page load. Any ideas? –  Poulomi Nag Oct 16 '12 at 7:07
    
If you changed the above to add_action('init', // etc) then it'll work on the page load rather than only in the admin section. –  darronz Oct 16 '12 at 7:19
2  
@darronz And then you need to check ! is_admin() inside, because the init hook runs on both sides. –  kaiser Oct 16 '12 at 11:20

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.