WordPress hooks for executing right before any action or page loading

Asked
Active3 hr before
Viewed126 times

8 Answers

wordpressexecutinghooks
90%

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? , 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 – Xavjer Apr 11 '13 at 12:31 , 2 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). – Factory Girl Apr 11 '13 at 12:19 , Check this codex.wordpress.org/Plugin_API/Filter_Reference and this codex.wordpress.org/Plugin_API/Action_Reference for all of the hooks. You can also create your own hooks , for that check archive.extralogical.net/2007/06/wphooks , wpengineer.com/1302/define-your-own-wordpress-hooks, sandeepthemaster.wordpress.com/2012/11/26/198 – Sabari Apr 11 '13 at 11:59

You can use it like this:

function my_function() {
   // your code goes here
}
add_action("template_redirect", "my_function");
load more v
88%

WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.,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 ?,Use template_redirect which is the action hook that fires before rendering the template;, Horror short story told through a collection of postcards about a man killed by an ancient snake goddess

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

add_action( 'wp', 'wpse69369_special_thingy' );
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 )
    );
}
load more v
72%

WordPress has a similar action called admin_init. It fires while the admin screen is being initialized, while the init action fires only after WordPress has finished loading.,Save this file and then activate the plugin in your WordPress dashboard. I’ll be using this plugin within a local WordPress installation to demonstrate how hooks work.,This solution only hides the specified admin menu items from appearing in the WordPress dashboard. All users can still access them by entering the menu URLs in their browsers.,After saving the plugin file, here’s a snapshot of the WordPress dashboard with an admin logged in.

Actions are defined in the WordPress code as:

do_action('action_name', [optional_arguments]);
load more v
65%

I want to add a filter function that is called on page load in PHP. For example, I would like to check some argument before the page is loaded. It must be able to check some $_SESSION variable. ,I recommend using an action hook with a priority to make your code load early. Check the following code as an example.,Next generation managed WP Hosting,I hope this helps. If you can provide more details on what you’re looking to accomplish here, I can try to help with a more specific piece of code to meet your needs.

function some_example() {
   $some_variable = some_value();
   if ($some_variable > 0) {
      // do something
   } else {
      // do something
   }
}
add_action('init', 'some_example', 1);
75%

Actions in WordPress allow us to add data or change the way in which WordPress or third party extensible code operates. Any callbacks (remember these are just functions) added to an action are run at a specific point when WordPress loads.,When hooking into an action in WordPress (adding your callback) there are two parts to the code.,WordPress looks for all the callback functions attached to this particular hook. Think about it as adding them to an invisible list.,WordPress now has a list of callback functions to run at this point in the load process and it does so, executing each callback function in turn after the previous one.

We have often found that clients want to include more information on the login page, for example, to remind users which email address they should use for logging in. Adding this below the login form is ideal. WordPress allows us to do this thanks to the use of an action. When WordPress outputs the login form content it includes the following line of code:

do_action('login_footer');

When WordPress outputs this (in the code) it does so with this code:

apply_filters(‘enter_title_here’, __(‘Enter title here’), $post);
load more v
40%

Enable and disable your changes easily because each piece of code is a small unit in your functions.php;,Make your changes relatively upgrade-proof because you no longer need to edit or copy WordPress or any themes and plugins core files;,The priority of this action: Whether it will run before, or after, other functions attached to the same hook. In this case we set the priority to 10, the default. If we want another function to run before this, we would give the other function a lower value (which means it will be executed first).,Change almost anything in WordPress—even quite fundamental things—because a lot of WordPress’s core functions use actions and filters;

// Send Gill to get paint
add_action( 'after_gill_arrives' , 'send_gill_to_get_paint', 10 , 2 );
function send_gill_to_get_paint( $gill_has_keys, $gill_has_car ) {
  // If $gill_has_keys and $gill_has_car are both true
  if ( $gill_has_keys && $gill_has_car ) {
    echo 'Gill, please go to the store and get some paint. Thank you!';
  }
}<br>
load more v
22%

Modifying the markup of the page right before the page loads.,Let’s break it down. A WordPress hook will let you know that something is happening. In other words, a hook will let you run custom codes when loading a WordPress website to add, remove or modify certain functionality or data. You can do the following with a hook:,Modifying the default admin footer text.,function(): This is the callback and the code that you want to execute in order to change something.

Basic WordPress hooks usage

add_action(‘init’, function() {
   // Do something.
});
load more v
60%

10 Useful WordPress Loop Hacks,Timing is everything: scheduling in WordPress,15 Useful Twitter Hacks and Plugins For WordPress,Once we have our function, we just hook it to existing WordPress functions.

A hook, as you would expect, “hooks” one function to another. For example, you could write a custom function and attach it to one of WordPress’ core functions:

add_action('publish_post', 'myCustomFunction');
load more v

Other "wordpress-executing" queries related to "WordPress hooks for executing right before any action or page loading"