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

时间:2019-03-16 13:53:08

标签: wordpress wordpress-hook

我想在文件wp();的{​​{1}}行之后运行一个函数,在这里使用正确的wp-blog-header.php是什么?

hook

我为什么需要钩子

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

  1. 在我们的<?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' ); } 配置文件中添加了以下rewrite规则:

NGINX

此规则将向URL(旧URL)添加一个额外的参数rewrite \D+(\/\d+\/\D+)$ /index.php?redirect=$1 break;,该参数的值将用于获取新的最终URL。

  1. 然后,我将运行以下代码从传入URL中获取此值,并通过查询2列表(将每个值redirect与最终URL redirect_from映射)获得最终URL:< / li>
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;} }

收件人:

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

0 个答案:

没有答案