如何在wp-blog-header.php文件中的wp()之后运行函数?

时间:2019-03-16 作者:Jafar Abazeed

我想在wp(); 文件中的行wp-blog-header.php, 什么是正确的hook 在这里使用?

<?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\' );

}

为什么我需要挂钩

我们正在迁移到一个新网站,我们必须关心旧的URL,所以我做了:

添加了以下内容rewrite 我们的规则NGINX 配置文件:

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

此规则将添加一个额外参数redirect 到URL(旧URL),使用一个值,我将使用该值获取新的最终URL。

然后,我将运行以下代码从传入URL中获取该值,并通过查询映射每个值的2列表来获取最终URLredirect_from 具有最终URLredirect_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:无法从旧URL获取新URL,以下是旧URL和新URL的示例:

重定向自:

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

收件人:

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

2 个回复
SO网友:Jacob Peattie

处理重定向的适当挂钩是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\' );

SO网友:Antti Koskinen

也许您可以将重定向函数放入一个必须使用的插件中,并将该函数挂接到muplugins_loaded. $那时wpdb应该是可用的,我认为它可以在需要重定向的情况下最大限度地减少加载的代码量。

您还可以查看操作参考https://codex.wordpress.org/Plugin_API/Action_Reference 找到其他挂钩。