最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

hooks - How to run a function after wp() in the wp-blog-header.php file?

matteradmin6PV0评论

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:

to:

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/category/sub-category/post-id/slug

to:

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

Share Improve this question edited Mar 16, 2019 at 15:23 Jaafar Abazid asked Mar 16, 2019 at 11:22 Jaafar AbazidJaafar Abazid 952 silver badges11 bronze badges 2
  • There’s lots of hooks that run at various places after that. What exactly do you want to do? – Jacob Peattie Commented Mar 16, 2019 at 14:48
  • Thank you @JacobPeattie , I've already updated the question, I appreciate any help. – Jaafar Abazid Commented Mar 16, 2019 at 15:24
Add a comment  | 

2 Answers 2

Reset to default 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' );

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/Plugin_API/Action_Reference to find other hooks.

Post a comment

comment list (0)

  1. No comments so far