注:从询问者处收集更多信息后,建议的方法将不时更新。当前建议的方法假设了一些条件。
以下代码用于输入functions.php
, 如果您正在编写插件。请记住更改回调并以插件模式写入。有关插件编写的更多信息,请参阅Plugin Handbook假设此示例
- the site domain is changed
- the whole url structure is same, 如果没有,则此解决方案不适用于情况1,如果您有一个旧站点,并且希望将任何页面重定向到只有不同域名的新站点。你可以试试这个
add_action( \'init\', \'q363843_global_redirect\' );
function q363843_global_redirect() {
// do redirect before sending header
if ( isset( $_SERVER[\'HTTP_HOST\'] ) ) {
$requested_url = is_ssl() ? \'https://\' : \'http://\';
$requested_url .= $_SERVER[\'HTTP_HOST\'];
$requested_url .= $_SERVER[\'REQUEST_URI\'];
}
// @ means surpass warning, for it is used in WordPress Core file like canonical.php, so I follow, for development, it is better to leave it
// $original = @parse_url( $requested_url );
$original = parse_url( $requested_url );
// change only domain, assume the new site is same in structure
// \'something-else\' must be different host for same host, an over simplified redirect without well checking will result in redirect loop, so beware
wp_redirect( $original[\'scheme\'] . \'://\' . \'something-else\' . $original[\'path\'] );
// exit will terminate the script immediately after redirect is called,
// this will ensure that the current script will be stopped here and prevent from infinite loop
// if use return here, it means finish the function but script is possible to continue in some occasions...
exit();
}
重要性exit()在重定向后放置exit()是在canonical中使用的WordPress核心实践。php、ms函数。php,可插拔。php和你的名字。...Terminates execution of the script...
关于return 关键字...return returns program control to the calling module.
案例2如果你想检查站点的帖子名称,你可以考虑以下内容,与案例1类似,只是你需要检查帖子名称。这是一种可能性,即机制:
(旧站点)首先重定向,因为新站点的结构不同,只需重定向到http://new-domain.com/post-name 首次使用(新站点)query
挂钩或pre_get_posts
分析并获得正确的帖子id。query
过滤器是最早处理查询的过滤器之一。在url_to_postid, 您可以从重定向链接中找到帖子id
我个人的工作经验和偏好,我选择
query
筛选并重新生成查询。使用的原因
query
因为
它在放入$wp\\u查询之前处理查询,因此尚未创建$post,此时的全局$post为空这是在任何404被检查和设置之前,如果设置正确并理解它,我可以解决任何404问题假设:
两侧都存在帖子名称,并且保持不变
// In the old site functions.php
add_action( \'init\', \'q363843_global_redirect\' );
function q363843_global_redirect() {
// do redirect before sending header
if ( isset( $_SERVER[\'HTTP_HOST\'] ) ) {
$requested_url = is_ssl() ? \'https://\' : \'http://\';
$requested_url .= $_SERVER[\'HTTP_HOST\'];
$requested_url .= $_SERVER[\'REQUEST_URI\'];
}
$original = parse_url( $requested_url );
// your extract post-name mechanism
$redirect = $original[\'scheme\'] . \'://\' . \'new-domain\' . \'/post-type/post-name\'; // <-- add post type if you know it, increase the matching possibility for url_to_postid()
wp_redirect( $redirect );
exit();
}
// In the new site functions.php
add_filter( \'query\', \'q363851_check_query_from_url\');
function q363851_check_query_from_url( $query ) {
// check and build the URL
if ( isset( $_SERVER[\'HTTP_HOST\'] ) ) {
$requested_url = is_ssl() ? \'https://\' : \'http://\';
$requested_url .= $_SERVER[\'HTTP_HOST\'];
$requested_url .= $_SERVER[\'REQUEST_URI\'];
}
// try to fetch post id using built-in function
$post = url_to_postid( $requested_url );
var_dump($post); // check to see
// here, you could either build the URL, redirect and exit this script (lazier) OR
// you modify the query so that WordPress think that the post type and page id is valid for next step
return $query;
}