duanhun3273 2018-10-26 19:43
浏览 165

无法使用wp_redirect重定向

So I saw some posts on here about this like: https://wordpress.stackexchange.com/questions/46336/wp-redirect-function-is-not-working I tried the solution to no avail. I wrote a php function that returns a permalink located in my functions.php file, the user enters a zip code, the form sends user to another page /locations where the function is called and the zip code is passed in:

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;
}

Search form on my main page looks like this:

<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" />

Then in my /locations page I call my function to get a permalink and the wp_redirect function to redirect to the outputted permalink like this:

<?php
    $zipcode = ( isset($_GET['zipcode']) ?  $_GET['zipcode'] : '');
    echo zip_search($zipcode);
    $url = zip_search( $zipcode );
    wp_redirect($url);
    exit();
?>

However when I try to search I get this error:

Warning: Cannot modify header information - headers already sent by (output started at /srv/bindings/bdc12884a5a545f1b029665fb0fc0d35/code/wp-includes/class.wp-styles.php:225) in /srv/bindings/bdc12884a5a545f1b029665fb0fc0d35/code/wp-includes/pluggable.php on line 1219

Am I calling this function in the wrong place? Has anyone run into this issue in the past?

  • 写回答

1条回答 默认 最新

  • dongtang1918 2018-10-26 20:01
    关注

    Please go through this article where you can fix your issue. I had same issue a few years back and it worked after looking into this post. Header Fix Issue

    While facing this issue, i had empty space before PHP statement opened.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?