Support » Plugins » Which hook will let me alter a Page template before it’s parsed?

  • Let’s say I created a very simple template for my custom theme:

    <?php get_header(); ?>
    <?php get footer();?>

    When I assign this template to a Page, I’d like to hook a plugin into a spot where I can parse this template and replace position1 and position2 with the contents of other Pages, or media, or anything else.

    What hook can I use to get in there and make that change? I don’t imagine I can use any filter hook since the template is a file, not some data from the database. I initially tried using the action hook “wp”, which seemed promising, but it’s executed for admin pages and I don’t see a non-hackish way to tell from my plugin whether the current page is an admin page or not

    That said, it seems like the “wp” hook could work, but is there a different one I should be using?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Could you use the init hook The hook runs after WordPress has finished loading but before any headers are sent.

    That might work, I will try it and compare to another candidate I found – “template_redirect”.


    The init hook would work ok, I think, but it seems to be about the same as the “wp” hook.

    I think that template_redirect will work better because the context that you’re dropped into (template-loader.php) appears to have the post data – $wp_query is in global scope with the post loaded up. So things like is_page(), get_the_ID(), etc. will return a value.

    Now to figure out when and how to inject code into that template…

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Which hook will let me alter a Page template before it’s parsed?’ is closed to new replies.