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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog