1

Hi I want to create a simple login code to redirect each user to his own profile page.

I'm using a page "CleanPage" as a template for my php code.

I tried header Location and wp_redirect but both shows this error:

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/general-template.php:1076) in /var/www/html/wp-content/themes/sydney/CleanPage.php on line 43

I wanted simple redirection like this:

header("Location: auth_customer.php?id=$userid");

Any help please?

Yosra MH
  • 129
  • 3
  • 11
  • 1
    try this: `header("Location: auth_customer.php?id=" . $userid);` – Ronnie Oosting Aug 27 '18 at 13:36
  • 1
    https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – popeye Aug 27 '18 at 13:36
  • 2
    you have to expand on your answer as to where exactly are you using this piece of code – popeye Aug 27 '18 at 13:37
  • @RonnieOosting also it shows the same problem (actually my code works well without wordpress, problems started when I switched to wp) – Yosra MH Aug 27 '18 at 13:41
  • @kenzotenma I have edited my post: I'm writing my code in a php page inside the theme (template) – Yosra MH Aug 27 '18 at 13:42
  • 1
    try `header("Location: 'FULL_PATH'/auth_customer.php?id=" . $userid);` – Ronnie Oosting Aug 27 '18 at 13:46
  • 1
    @RonnieOosting I don't think this will ever work. she's trying to send header information alongside the main content. any kind of header information has to be sent before sending the main response. – popeye Aug 27 '18 at 13:53
  • 1
    you should give this a read: https://codex.wordpress.org/Plugin_API/Action_Reference/send_headers -- this may solve your problem – popeye Aug 27 '18 at 13:53

5 Answers5

3

though I've already posted the link in the comment section above. I believe this should definitely solve your problem

<?php
add_action( 'send_headers', 'add_header_xua' );
function add_header_xua() {
    // your login logic or whatever should go here
    // ....
    header("Location: auth_customer.php?id=$userid");
}
popeye
  • 481
  • 1
  • 4
  • 16
  • also this code still give me the same warning :( I used it in my CleanPage.php, should I write it on functions.php? – Yosra MH Aug 27 '18 at 14:13
  • I can see the logic: it's as u said, sending my custom header before calling that 'wp_head();' but even this didn't work :(( maybe it's my bad that I didn't know how to use it – Yosra MH Aug 28 '18 at 10:16
1

Add an action to "wp_loaded" with a custom function and then make redirect within it.

<?php
add_action ('wp_loaded', 'ss_custom_redirect');
function ss_custom_redirect() {
    $redirect = 'http://example.com/redirect-example-url.html';
    wp_redirect($redirect);
    exit;
}     
?>
Shashank Sharma
  • 585
  • 3
  • 14
  • I still get that worning: Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/general-template.php:1076) in /var/www/html/wp-includes/pluggable.php on line 1219 – Yosra MH Aug 27 '18 at 13:54
1

The easiest one is to use 'meta refresh', try like this <?php echo "<meta http-equiv='refresh' content='0;url=auth_customer.php?id=$userid'>"; ?>.

Outsource WordPress
  • 3,749
  • 2
  • 10
  • 23
1

Try this:

wp_redirect( home_url('/auth_customer.php?id='.$userid) ); exit;
Bijaya Kumar Oli
  • 2,007
  • 19
  • 15
1

You need to place the redirect function before get_header() / wp_head() for example:

if( //some logic here ) { 
  wp_redirect('page1');
} else {
  wp_redirect('page2');
}
...
get_header();
Ash0ur
  • 1,505
  • 2
  • 8
  • 11
  • 1
    There must be something is sent / printed from the server before the redirect, maybe happens before calling the custom template. – Ash0ur Aug 28 '18 at 11:31