0

I want to run a function after the wp(); line in the file wp-blog-header.php, what is the proper hook to use here?

<?php
/**
 * Loads the WordPress environment and template.
 *
 * @package WordPress
 */

if ( !isset($wp_did_header) ) {

    $wp_did_header = true;

    // Load the WordPress library.
    require_once( dirname(__FILE__) . '/wp-load.php' );

    // Set up the WordPress query.
    wp();

    /********************************************
       I WANT TO RUN THE FUNCTION AT THIS POINT 
     ********************************************/

    // Load the theme template.
    require_once( ABSPATH . WPINC . '/template-loader.php' );

}

Why do I need the hook

We are migrating to a new website and we have to care about the old URLs, so what I did:

  1. Added the following rewrite rule to our NGINX config file:

rewrite \D+(\/\d+\/\D+)$ /index.php?redirect=$1 break;

This rule will add an extra parameter redirect to the URL (old URL) with a value that I will be using to get the new final URL.

  1. Then I will run the following code to get this value from the incoming URL and get the final URL by querying a 2-columns table that maps each value redirect_from with a final URL redirect_to:
/**
 * 1. Check if the URL has a parameter [redirect]
 * 2. If NO, proceed to the next step
 * 3. If YES, then get that parameter value and look into [redirects] table
 * 4. If you found a row that has that value, then get the [redirect_to] value
 * 5. Redirect to that URL [redirect_to]
 */

if (isset($_GET['redirect'])) {
    // Get the parameter value from the URL
    $redirect_from = $_GET['redirect'];
    // Add the table prefix to the table name
    $table_name = $wpdb->prefix . 'redirects';
    // The SQL query
    $query = "
        SELECT redirect_to
        FROM $table_name
        WHERE redirect_from = '$redirect_from';
    ";
    // Run the SQL query and get the results
    $result = $wpdb->get_results($query, OBJECT);

    // If there was a result then do the redirection and exit
    if (wp_redirect($result[0]->redirect_to)) {exit;}
}

Note: No way to get the new URLs from old URLs, here is an example of the old and new URLs:

Redirect from:

http://www.example.com/category/sub-category/post-id/slug

to:

https://www.example.com/category/sub-category/yyyy/mm/dd/slug

  • There’s lots of hooks that run at various places after that. What exactly do you want to do? – Jacob Peattie Mar 16 '19 at 14:48
  • Thank you @JacobPeattie , I've already updated the question, I appreciate any help. – Jafar Abazeed Mar 16 '19 at 15:24
1

The appropriate hook for handling redirects would be template_redirect:

function wpse_331804_redirects() {
    /**
     * 1. Check if the URL has a parameter [redirect]
     * 2. If NO, proceed to the next step
     * 3. If YES, then get that parameter value and look into [redirects] table
     * 4. If you found a row that has that value, then get the [redirect_to] value
     * 5. Redirect to that URL [redirect_to]
     */

    if (isset($_GET['redirect'])) {
        // Get the parameter value from the URL
        $redirect_from = $_GET['redirect'];
        // Add the table prefix to the table name
        $table_name = $wpdb->prefix . 'redirects';
        // The SQL query
        $query = "
            SELECT redirect_to
            FROM $table_name
            WHERE redirect_from = '$redirect_from';
        ";
        // Run the SQL query and get the results
        $result = $wpdb->get_results($query, OBJECT);

        // If there was a result then do the redirection and exit
        if (wp_redirect($result[0]->redirect_to)) {exit;}
    }
}
add_action( 'template_redirect', 'wpse_331804_redirects' );
|improve this answer|||||
  • Thank you @JacobPeattie, I think this will work, I will try it and then flag it as the accepted answer if it worked. – Jafar Abazeed Mar 21 '19 at 8:28
0

Perhaps you could put your redirect function into a must-use plugin and hook the function to muplugins_loaded. $wpdb should be available then and I think it would minimize the amount of code loaded in the case a redirect is needed.

You can also have a look at the action reference https://codex.wordpress.org/Plugin_API/Action_Reference to find other hooks.

|improve this answer|||||
  • Thank you @AnttiKoskinen, actually, I don't want to go with plugins because we have about 120K redirections that could cause a performance issue if I went with a plugin. – Jafar Abazeed Mar 21 '19 at 8:33
  • My thinking was that you could potentially make the redirect faster by putting the redirect function into a must-use plugin (a single file in wp-content/mu-plugins) and doing the redirect check on muplugins_loaded. If you look at the Action Reference, you'll see that muplugins_loaded is the first action on the list whereas template_redirect is the 27th (if I counted correctly). – Antti Koskinen Mar 21 '19 at 11:18
  • If $_GET['redirect'] isset and there's a match from the custom DB table, the redirect would kick-in and it would happen way before for example init, which fires before template_redirect and is commonly used by theme and plugins to initialize stuff. I think the DB query you're doing inside your redirect function takes the same time regardless which action it is hooked to. But by using an action that fires earlier you'd minimize the amount of included and processed PHP. – Antti Koskinen Mar 21 '19 at 11:19
  • It worths a try, thanks a lot @AnttiKoskinen. – Jafar Abazeed Mar 21 '19 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.