Solved

WordPress & php header

Posted on 2015-02-13
17
115 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross domain request in Javescript 9 45
Wordpress Body Class 5 33
Form Submit falis 6 33
Seeking info on how to integrate RETS feed based MLS data into a WordPress site 5 43
Now that you've installed WordPress 2.9 (http://www.experts-exchange.com/articles/Web_Development/Blogs/WordPress/WordPress-2-9-What-to-Expect-When-Upgrading-to-WordPress-2-9.html?) on your site, you need to install some plugins to get the most out …
How to install BuddyPress on your self-hosted WordPress site It’s been called everything from “social networking in a box” to “Facebook without the terms of service,” but until Feb. 16, BuddyPress was a relatively unknown outside the WordPress MU…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

776 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