Solved

WordPress & php header

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
How to create your own WordPress Theme Before I begin with the theme design tutorial, I would like to give you the basics of what is required to make a WordPress Theme. The most basic WordPress theme only requires 2 files: a style.css file and an…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

920 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

14 Experts available now in Live!

Get 1:1 Help Now