0

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 を気にする必要があるため、私がしたことは次のとおりです。

  1. rewrite次のルールをNGINX構成ファイルに追加しました。

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

このルールは、新しい最終 URL を取得するために使用する値を持つ追加パラメーターredirectを URL (古い URL) に追加します。

  1. 次に、次のコードを実行して受信 URL からこの値を取得し、各値redirect_fromを最終 URL にマップする 2 列のテーブルをクエリして最終 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;}
}

注: 古い 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

4

0 に答える 0