5

I wrote some code, that helps secure my clients files, and added it to wordpresses function file. however, whenever updates come in, it obviously overwrites my function.

So I wanted to create it as a plugin.

However, I keep getting this error:

PHP Warning:  Cannot modify header information - headers already sent by...

So I need to execute my code, BEFORE wordpress sends the headers.

How do I do that?

Thanks, Richard

Update. Okay, here is the code, I changed the tags though, but same premise...

<?php 
/*
 * Plugin Name: My WP Plugin
 * Plugin URI: http://www.example.com/plugins
 * Description: My Plugin
 * Version: 1.0
 * Author: My Name
 * Author URI: http://www.example.com/
*/

function somebit_init() {
    $_permaStruc = get_option('permalink_structure');
    if($_permaStruc != "") {
        if($_GET['dl']) {
            header("Location: http://google.com");
            exit;
        } else if($_GET['download']) {
            header("Location: http://google.com");
            exit;
        }
    }
}
add_action('init', 'somebit_init');
?>

I'm still getting the "PHP Warning: Cannot modify header information - headers already sent by..." error.

Do you see why? I cannot find it. maybe I did something wrong that I cannot see.

Richard

14
  • How are you calling your code? What action/filter are you hooking into? Jan 13, 2015 at 18:55
  • @czerspalace, I updated the post above, it now shows the entire code, I have just updated, but still having a error.
    – Richard
    Jan 13, 2015 at 19:08
  • The even crazier thing is, that even if I just put the check and do not do anything, no action in the if statement, I still get the headers already sent error. But if I'm not printing anything to the page, or trying to set a header, or trying to redirect, why would it get that error? I don't get it.
    – Richard
    Jan 13, 2015 at 19:15
  • Now, I ONLY added the function: code function somebit_init() {/* Do Nothing */ } add_action('template_redirect', 'somebit_init'); code and I still get the error.
    – Richard
    Jan 13, 2015 at 19:17
  • What if you remove the ?> that is after the add_action Jan 13, 2015 at 19:38

5 Answers 5

6

The correct hook to use is template_redirect which allows you to have the necessary info available to do checks while also early enough to actually redirect. As per the example on the codex page:

function my_page_template_redirect()
    {
    if( is_page( 'goodies' ) && ! is_user_logged_in() )
    {
        wp_redirect( home_url( '/signup/' ) );
        exit();
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Codex page here - template_redirect

3

How about using this action? Codex Link - send_headers action

add_action( 'send_headers', 'add_redirect_header' );
function add_redirect_header() {
    header( 'Location: http://www.google.com' );
}
2

use add_action('init', 'your_function');

or any action hook before headers are sent: http://codex.wordpress.org/Plugin_API/Action_Reference

5
  • template_redirect can be used if the function handling the redirect depends on WP being loaded Jan 13, 2015 at 19:01
  • @karpstrucking I tried that, same error. "PHP Warning: Cannot modify header information - headers already sent by..." the files it says already sent them, is the code I updated on the original post... but I never started any headers.
    – Richard
    Jan 13, 2015 at 19:11
  • when does this error appear, when you try to activate the plugin? can you post the whole error, including file path info Jan 13, 2015 at 19:13
  • It appears in the error log. On the wordpress site, I get a blank page. nothing. Here is the whole error string, only changed the username in the path... code"[13-Jan-2015 19:16:35 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/username/public_html/hvac/wp-content/plugins/My-Security-Plugin/my_security_security.php:1) in /home/username/public_html/hvac/wp-includes/pluggable.php on line 876"code
    – Richard
    Jan 13, 2015 at 19:23
  • the plugin is activated. it did say that there were 3 unexpected characters output when activated, but it stayed active and no errors and the page loads, but the error happens when I put the dl=filename.zip or anything with ?dl= in the url.
    – Richard
    Jan 13, 2015 at 19:24
0

Your checks, if($_GET['dl']) and else if($_GET['download']) are throwing Undefined index: errors, and that appears to be causing a headers issue with your My Security Plugin.

Try changing it to:

function somebit_init() {
    $_permaStruc = get_option('permalink_structure');
    if($_permaStruc != "") {
        if( !empty($_GET['dl'])) {
            header("Location: http://google.com");
            exit;
        } else if( !empty($_GET['download'])) {
            header("Location: http://google.com");
            exit;
        }
    }
}
0

This is a function I used to check if a non logged in user is trying to enter directly in custom page (only for users), it check if someone entered certain page.

Even makes a second check if the main cookies are created (useful to avoid undefined index errors)

    function validate_sesion() {
    if ((is_page('something'))||(is_singular('something'))) {
        if (!is_user_logged_in()) {
            wp_redirect(home_url('log-out'));
            exit();
        } elseif ((empty($_COOKIE["user_id"])) || (empty($_COOKIE["user_role"]))) {
            if (is_user_logged_in()) {
                wp_redirect(home_url('log-out'));
                exit();
            }
        }
    }

Just like the other answers, I used the 'template_redirect' hook

    add_action('template_redirect', 'validate_sesion');

I hope it can help someone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.