Solved

WordPress & php header

Posted on 2015-02-13
17
118 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Like there are many different versions of Wordpress, there are also a handful of related side projects WordPress users should be familiar with.   All of these projects are open source and GPL licensed, which means there are few restrictions to us…
WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

695 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