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

5 s
5

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

Leave a Reply

Your email address will not be published. Required fields are marked *