wordpress链接重定向跳转函数wp_redirect()

WordPress教程 5167

wp_redirect()是重定向到指定url的wordpress函数,简单地理解就是通过该函数可以实现页面跳转到指定URL地址,并可设置指定的状态码,如301、302等。

函数代码

1
wp_redirect( string $location, int $status = 302, string $x_redirect_by = 'WordPress' )
参数说明:
  • $location – 字符串,必选,要重定向到的URL,需要是绝对URL地址,不能是相对地址,默认值为空(该值为空的,返回false)
  • $status – 整数,可选,跳转时的HTTP状态码,默认为302(临时重定向状态码)
  • $x_redirect_by – 字符串,可选,执行重定向的应用,默认值为“WordPress”

示例

1
2
3
4
<?php 
	wp_redirect('https://www.boke8.net/', 301);
	exit; 
?>

提示:因为wp_redirect()函数不会自动退出,所以添加了exit,如果需要继续执行则不需要添加。

函数位置:wp-includes/pluggable.php

wordpress官方文档:https://developer.wordpress.org/reference/functions/wp_redirect/

精品推荐: