我的functions.php檔案中有以下代碼,用于在我的整個站點上提供臨時重定向(所有 URL 都重定向到主頁),但不包括 wordpress 管理區域...
add_action( 'template_redirect', 'wa_redirect_site', 5 );
function wa_redirect_site(){
$url = 'https://' . $_SERVER[ 'HTTPS_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
$current_post_id = url_to_postid( $url );
if ( ! is_page( )) { // Excludes page with this ID, and also the WordPress backend
$id = get_the_ID();
wp_redirect( 'https://wa-leicester.org.uk/', 302 ); // You can change 301 to 302 if the redirect is temporary
exit;
}
}
到目前為止,一切都很好,但是,我在 Wordpress 管理區域中使用了一個名為 Bricks Builder 的頁面構建器,問題是,當我創建一個模板并嘗試編輯該模板時,它會重定向到主頁。
編輯模板時,它嘗試訪問的 URL 是:.../template/115/?bricks=run
這給了我一個 ID,但我認為我需要找到一種方法來排除整個 /template/ 目錄。
該代碼包含一種排除頁面ID的方法,is_page( ))因此我嘗試將目錄添加到該目錄中,并且我還嘗試查看是否存在替換該目錄的內容,例如:is_directory但我似乎找不到任何關于它的內容。
任何幫助將不勝感激!
uj5u.com熱心網友回復:
您可以使用is_admin()來確定當前請求是否針對管理界面頁面。默認的 WordPress 定制器不被視為管理頁面。
此外,很多插件都在使用 Ajax 請求(@see AJAX in Plugins),因此希望將其過濾掉。
默認情況下,wp_redirect(跨站點)或wp_safe_redirect(本地)回傳 302。
<?php
add_action( 'template_redirect', function () {
/**
* Determines whether the current request is NOT for an administrative interface page NOR a WordPress Ajax request.
* Determines whether the query is NOT for the blog homepage NOR for the front page of the site.
*
* is_admin() returns true for Ajax requests, since wp-admin/admin-ajax.php defines the WP_ADMIN constant as true.
* @see https://developer.wordpress.org/reference/functions/is_admin/#comment-5755
*/
if ( ! is_admin() && ! wp_doing_ajax() && ! is_home() && ! is_front_page() && ! is_user_logged_in() ) {
/**
* Performs a safe (local) redirect, using wp_redirect().
*
* By default the status code is set to 302.
* @see https://developer.wordpress.org/reference/functions/wp_safe_redirect/#parameters
*/
wp_safe_redirect( home_url() );
exit;
};
} );
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/515774.html
上一篇:回傳通用樹節點n的高度(遞回)
