WordPress hooks for executing right before any action or page loading

49,777

Solution 1

A list of all available hooks can be found here: https://codex.wordpress.org/Plugin_API/Action_Reference

Information about Hooks: https://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters

Other hooks must be suggested and will be added in a future release if is a good suggestion. Or you'd have to edit the core files ;)

Solution 2

Last hook before loading the template is template_redirect

You can use it like this:

function my_function(){
    // your code goes here
}
add_action( "template_redirect", "my_function" );

Solution 3

You can use 'init' hook. It will be performed before element or html code. It`s also useful to manage POST and GET variables. The syntax is something like this:

function yourfunction() {
    dosomething();
}
add_action('init', yourfunction);
Share:
49,777
Factory Girl
Author by

Factory Girl

Updated on July 05, 2022

Comments

  • Factory Girl
    Factory Girl almost 2 years

    I'm quite new to WP. Task is to develop a plugin for oauth authentication on one of not popular openID providers. I did the same for CodeIgniter project, but WP is a CMS and is little bit complex for me to understand. In Codeigniter i check authorisation before each action. In WP i need a hook which uses for it... before each page printing, or maybe.. it would be right to say before each action in terms of frameworks. What is this hook's name?

  • Factory Girl
    Factory Girl about 11 years
    I browsed these pages in Codex before questioning here. And couldn't find any suitable hook. I need smth which will be executing right before any action(in terms of frameworks).
  • Xavjer
    Xavjer about 11 years
    Why should you need to execute that b4 any action? just to get the point, you want to run the check at the beginning of everything... not run it once before every possible action (1000times ;) ), if you want to run it at the earliest point possible I suggest loop_start or init
  • Factory Girl
    Factory Girl about 11 years
    maybe some hook is available for calling before loading every page?
  • Xavjer
    Xavjer about 11 years
    How about muplugins_loaded After must-use plugins are loaded (this is the earliest hook you could use, [the order of the hooks is the order in which they are called)
  • Factory Girl
    Factory Girl about 11 years
    This hook is used before loading each page?
  • Factory Girl
    Factory Girl about 11 years
    or just one time in first visit of user?
  • Xavjer
    Xavjer about 11 years
    Each time you send a request to the server (klick a link, change page etc), so as you said before loading the page, every time!
  • Factory Girl
    Factory Girl about 11 years
    Ok. Thanks. I'll try this hook
  • plushyObject
    plushyObject over 7 years
    The second parameter is a string. Perhaps this has changed since this answer.
  • Marc van Nieuwenhuijzen
    Marc van Nieuwenhuijzen about 7 years
    I voted this down because there is a hook that fires "right before" page loading and it's not "init". It is "template_redirect". I created a new answer for this.
  • rafaelfndev
    rafaelfndev about 5 years
    I don't know why but "init" hook are executing two times, and "template_redirect" run just one, thanks.