php - 无法使用 wp_redirect 重定向
问题描述
所以我在这里看到了一些关于这个的帖子:https ://wordpress.stackexchange.com/questions/46336/wp-redirect-function-is-not-working
我尝试了解决方案无济于事。我编写了一个 php 函数,该函数返回位于我的functions.php
文件中的永久链接,用户输入邮政编码,表单将用户发送到/locations
调用该函数并传入邮政编码的另一个页面:
function zip_search($userZip){
$args = array(
'posts_per_page' => -1,
'post_type' => 'Locations'
);
$wp_query = new WP_Query($args);
if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post();
$zipField=get_field('zip_codes_services');
$zipString = $zipField . ', ';
$array = explode(', ' , $zipString); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
if($value==$userZip){
$post_id = get_the_ID();
$permalink=get_permalink($post_id);
return ($permalink); //print
}
}
endwhile;
wp_reset_postdata();
endif;
}
我主页上的搜索表单如下所示:
<form action="/locations" method="get">
<input class="form-control search-input"
autocomplete="off"
name="zipcode"
type="text"
value=""
placeholder="Enter Zip Code" />
</form>
<input type="submit" />
然后在我的/locations
页面中,我调用我的函数来获取永久链接,并调用 wp_redirect 函数来重定向到输出的永久链接,如下所示:
<?php
$zipcode = ( isset($_GET['zipcode']) ? $_GET['zipcode'] : '');
echo zip_search($zipcode);
$url = zip_search( $zipcode );
wp_redirect($url);
exit();
?>
但是,当我尝试搜索时,出现此错误:
警告:无法修改标头信息 - 标头已由 /srv/bindings/bdc12884a5a545f1b029665fb0fc0d35/code/wp-在第 1219 行包含/pluggable.php
我在错误的地方调用这个函数吗?过去有没有人遇到过这个问题?
解决方案
请阅读这篇文章,您可以在其中解决您的问题。几年前我也遇到过同样的问题,在查看这篇文章后它起作用了。 标题修复问题
在面对这个问题时,我在 PHP 语句打开之前有空白空间。
推荐阅读
- c - 是否可以在 Windows 中使用 C 更改我的 IP 地址?
- linux - Vagrant 无法在 Ubuntu 20 客户机中挂载 NFS
- javascript - 避免在 Nuxt VueJs 中直接改变道具
- reactjs - React - 在宽屏上显示所有路线,但在移动设备上只显示一条
- javascript - 是的架构:如何在错误消息中添加新行
- c# - C# 中的 UUID_To_BIN 等效项
- oauth-2.0 - PKCE:为什么要对代码验证器进行哈希处理?
- javascript - 在行中使用 HTML 下拉菜单后,从 R Shiny 中的 DT 表中获取当前显示的数据
- laravel - 如何在 laravel 8 中实现多外键
- python - 如何访问特定数组?