2

I have three dropdown filters on the front site to select country, territory and region and need to redirect the user to the country/territory/region page depending upon the filter values on click of Submit button.

FYI, I have pages with the same slug.eg- example.com/country-name/territory-name/region-name

I am trying to use wp_redirect( $final_url ); exit; but it is throwing a warning-

Warning: Cannot modify header information - headers already sent by (output started at E:\path\to\root-folder\wp-includes\class.wp-styles.php:154) in E:\path\to\root-folder\wp-includes\pluggable.php on line 1121

Please help.

Can someone suggest me some some better way?

//Edit- here is my code

if(isset($_POST['submit']) and $_POST['action']=='findurl')
    {
        //Result $POST Array ( [countrySelect] => 1 [territorySelect] => 1 [regionSelect] => 2 )
        if (isset($_POST['countrySelect'])){

            $rows_country_url = $wpdb->get_results("select url from country where id=".$_POST['countrySelect']."");
        }

        if (isset($_POST['territorySelect'])){

            $rows_territory_url = $wpdb->get_results("select url from territory where id=".$_POST['territorySelect']."");
        }

        if (isset($_POST['regionSelect'])){

            $rows_region_url = $wpdb->get_results("select url from region where id=".$_POST['regionSelect']."");
        }


        if($rows_country_url[0]->url !=''){
        $final_url = home_url('/').$rows_country_url[0]->url;}

        if($rows_territory_url[0]->url !=''){
        $final_url = $final_url.'/'.$rows_territory_url[0]->url;}

        if($rows_region_url[0]->url !=''){
        $final_url = $final_url.'/'.$rows_region_url[0]->url;}

        echo $final_url;

        if($final_url!=''){ 
        wp_redirect( home_url() ); exit;
        echo "not blank";
        }else{ echo "Please select values";}
    }
2
  • Where are you using wp_redirect? You can't output any HTML and then call wp_redirect. It must be called before you output any content to the browser. Show some code.
    – Todd Rowan
    Jun 27, 2014 at 8:35
  • Hi Todd, I have edited my question to show the code. Please have a look. Jun 27, 2014 at 8:54

2 Answers 2

1

Please make sure these is no data output or blank spaces above wp_redirect( $final_url ); exit; otherwise this warning will always appear.

Also optionally you can use

<?php
//Php code 
 ?>
<script type="text/javascript">
      document.location.href="http://example.com";
</script>
<?php
//php code
?>
2
  • this script works like a charm and solved my purpose. Not sure why wp_redirect not working after removing the echo statements and removing spaces between the arguments. My from action is <?php the_permalink()?> Should I need to call any function and put this code in it to use wp_redirect? Jun 27, 2014 at 9:40
  • There can be any data output before redirect , You need to debug alot where it is exactly. Jun 30, 2014 at 15:23
0

My solution is.

  1. Create a function for javascript redirect.

    function rm_redirect($url){ $string = ''; $string .= 'window.location = "' . $url . '"'; $string .= ''; echo $string; }

  2. Call this function whenever you want like this.

    rm_redirect('http://example.com');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.