Solved

WordPress & php header

Posted on 2015-02-13
17
112 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 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Richard Korts
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, get_header() also ruins wp_redirect() because of headers already sent issues.
0
 

Author Comment

by:Richard Korts
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
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
Comment Utility
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
Comment Utility
Jason,

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

Richard
0
 
LVL 70

Expert Comment

by:Jason C. Levine
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you are looking for plug-ins to add functions to your WordPress small business web site, take some time to read though this comprehensive list.  These are all the plugins I use for my customers WordPress web sites, as well as my own.  Be sure to …
I want to start by talking about the use of plug-ins for WordPress. I started a web-site for a company I was working for a few years ago; I had extremely basic knowledge of HTML. I am a Graphic Designer by trade so I invited the opportunity as a cha…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
This video teaches users how to migrate an existing Wordpress website to a new domain.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now