Which hook will let me alter a Page template before it's parsed? (4 posts)

  1. nickpdx
    Posted 6 years ago #

    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?

  2. tomontoast
    Posted 6 years ago #

    Could you use the init hook http://adambrown.info/p/wp_hooks/hook/init The hook runs after WordPress has finished loading but before any headers are sent.

  3. nickpdx
    Posted 6 years ago #

    That might work, I will try it and compare to another candidate I found - "template_redirect".


  4. nickpdx
    Posted 6 years ago #

    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...

Topic Closed

This topic has been closed to new replies.

About this Topic