我想在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