Solved

WordPress & php header

Posted on 2015-02-13
17
117 Views
Last Modified: 2015-02-14
Does php header not work in WordPress/

I have this at the top of a custom page:

<?php get_header();
      //echo "bft = " . $_POST['bft'] . "<br>";
            if ($_POST['bft'] == "n") {
                  header("Location: http://www.rain1.com/new-site/service-plans/?basic=" . $_POST['basic'] . "&recommend=". $_POST['recommend'] . "&aeration=" . $_POST['aeration'] );
            exit;
            }
?>

Before I put the exit in, it just displayed the page this is at the top of. Now it displays a "blank" page.

I'm guessing it's the "headers already sent" thing.

How can I accomplish this another way?

Thanks
0
Comment
Question by:Richard Korts
  • 8
  • 7
  • 2
17 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 40609089
It works if you do it right.  That code looks pretty awful.  You're not validating any of your $_POST inputs.  And you are not urlencoding any of the data.  I suggest you try 'echoing' that URL to see what you are really creating.  http://php.net/manual/en/function.urlencode.php
0
 

Author Comment

by:Richard Korts
ID: 40609158
Dave,

I url encoded the Query string; no change.

So I'm giving up on that.

Watch for my next one, this project is a NIGHTMARE!!!
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40609251
I'm guessing it's the "headers already sent" thing

Yes.  Don't send headers again, use wp_redirect()

http://codex.wordpress.org/Function_Reference/wp_redirect
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Richard Korts
ID: 40609934
To Jason,

wp_redirect doesn't work.

I used this code (at the TOP of the page):

<?php get_header();
      //echo "bft = " . $_POST['bft'] . "<br>";
            if ($_POST['bft'] == "n") {
                  //$qs = "?basic=" . $_POST['basic'] . "&recommend=". $_POST['recommend'] . "&aeration=" . $_POST['aeration'];
                  //$qs = urlencode($qs);
                  //$url = "http://www.rain1.com/new-site/service-plans/" . $qs;
                  //echo "url = " . $url . "<br>";
                  wp_redirect("http://www.rain1.com/new-site/rkorts-test/");
                  exit;
                  //header("Location: " . $url );
            }
?>
Produces a blank page with the url of the page the wp_redirect is in (I guess because of the exit).

Or does the code have to be down later?

Thanks
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40609945
You misunderstand how to use 'urlencode'.  It is supposed to encode only the query data, not the entire string.
0
 

Author Comment

by:Richard Korts
ID: 40609951
Dave,

As per Jason, I am trying now to use wp_redirect. Note the other stuff is all commented out.

Notice, enclosed in the if,

 if ($_POST['bft'] == "n") {

The only non-commented out php is:

 wp_redirect("http://www.rain1.com/new-site/rkorts-test/");
 exit;

which doesn't work.

Thanks
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40609990
Sorry, get_header() also ruins wp_redirect() because of headers already sent issues.
0
 

Author Comment

by:Richard Korts
ID: 40609998
Jason,

It's a WordPress page, one of those I made per your instructions a few days back.

You know, the one about "and then have a beer".

Whole page attached.

Notice I put it just after get_header. I'm thinking maybe it needs to go farther down, just ahead of my other custom code.

Thanks

Richard
page-service-plans-with-backflow-test.ph
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40610008
No, see my edited answer.  I looked at wp_redirect() and all it does is wrap a header() function. I need to think this through a bit more as there probably needs to be a hook involved.
0
 

Author Comment

by:Richard Korts
ID: 40610026
Jason,

If I can get Javascript to work I can avoid this. But I'd like to know how to make it work

Seems AMAZING that you cannot redirect in Word Press.

Thanks

Richard
0
 

Author Comment

by:Richard Korts
ID: 40610029
Jason,

Why can't I put wp_redirect IN FRONT of get_header()?

Richard
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40610051
Yes, I'm right about needed ink to hook this. I hate writing code on a iPad so i'll post a snippet after 2:30 or so later today
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40610059
Why can't I put wp_redirect IN FRONT of get_header()?

You could try.  It might still trigger an error because there's more going on with WordPress before get_header() depending on theme and plugins.  Using a hook attached to a low-level action will ALWAYS work.
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40610071
Seems AMAZING that you cannot redirect in Word Press.

This is a PHP thing, not a WordPress thing.  You can't send headers after headers have already been sent.  WordPress just adds some complexity so it's hard to know when a header has already been sent sometimes.
0
 

Author Comment

by:Richard Korts
ID: 40610176
Jason,

Thanks.

I know about the headers thing, I learned that years ago with php.

The frustrating thing about WordPress is you (at least I) cannot see what's going on.

My son uses Word Press; he loves it but he's NOT technically (programming wise) inclined. I ask him how to do things like this, his standard answer, there's a plug-in for that).

I'm hoping after this experience, I will understand a LOT about how to integrate WordPress with "normal" web programming.
0
 

Author Comment

by:Richard Korts
ID: 40610185
Jason,

I put the wp_redirect BEFORE the get_header();

It WORKED!!!

But if you have a more WordPress friendly solution, I'm all ears.

Richard
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 40610315
Put this, or something like it in functions.php:

add_action( 'wp_loaded', 'my_form_redirection' );
function my_form_redirection() {
    if( your conditional here ) {
            wp_redirect(); // add link here
            exit();
    }
}

Open in new window


This is "better" in the WordPress sense than going above get_header() because you are firing the redirection with the wp_loaded action:

http://codex.wordpress.org/Plugin_API/Action_Reference/wp_loaded

which will guarantee that no other headers have been sent yet.  It also has the benefit of loading through functions.php or a class that can be called as a require() in functions.php which makes code organization/maintenance easier.

If you are going to be developing inside WordPress, it is critical to learn how to use Actions and Filters by hooking into them:

http://codex.wordpress.org/Glossary#Action
http://codex.wordpress.org/Glossary#Filter
http://codex.wordpress.org/Plugin_API/Action_Reference

The "problem" with custom code at the page template level is that it's literally the last thing to run and you sometimes find yourself in conflict with theme and plugin calls which are executing above.  The links above will give you a quick overview of the process but this is a book-style learning job and will take a while.

You can't approach any CMS as a file-by-file coding job.  You first have to learn the architecture...

The frustrating thing about WordPress is you (at least I) cannot see what's going on.

You can if you enable wp_debug in wp-config.php

http://codex.wordpress.org/Debugging_in_WordPress
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"Go Fund Me" type plugin 5 43
WordPress Madness 8 43
jQuery Toggle & Anchor Links 5 56
Access blocked to XML files other than sitemap.xml? 4 36
WordPress has made it easy to create scalable sites based on their powerful CMS, but as great as WordPress is, there are some SEO issues that haven’t been addressed out of the box.  Fortunately these problems can be solved with a few plugins.  The f…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question