I want to write a simple Wordpress plugin. The plugin will:
- Read (if set) a cookie from the visitor
- Send a cookie (if not set) to the visitor
- Depending on the cookie value I will redirect the user to a new pages
I found some documentation for getting/setting cookies through WP, and I'm guessing I can just issue a redirect from PHP. So I downloaded the WordPress Boilerplate (WPBP) code and so far so good. But I'm stuck at:
- Which WP callback should I hook into for my code? (init ?) I found the plugin documentation but I'm struggling to understand where this type of code should hook into.
- Should I put any of my code into the main/global portion of the WPBP file? Or only in the called back function.
- This plugin will run on a WP multisite installation. Does that mean my plugin code would run on every site? If so, how could I restrict my plugin code to run on only a particular site? (PHP check for a URI?)
I want to write a simple Wordpress plugin. The plugin will:
- Read (if set) a cookie from the visitor
- Send a cookie (if not set) to the visitor
- Depending on the cookie value I will redirect the user to a new pages
I found some documentation for getting/setting cookies through WP, and I'm guessing I can just issue a redirect from PHP. So I downloaded the WordPress Boilerplate (WPBP) code and so far so good. But I'm stuck at:
- Which WP callback should I hook into for my code? (init ?) I found the plugin documentation but I'm struggling to understand where this type of code should hook into.
- Should I put any of my code into the main/global portion of the WPBP file? Or only in the called back function.
- This plugin will run on a WP multisite installation. Does that mean my plugin code would run on every site? If so, how could I restrict my plugin code to run on only a particular site? (PHP check for a URI?)
1 Answer
Reset to default 1Your questions about how you should structure your plugin are somewhat too broad, but here is a specific answer to the title question.
To change headers before they’re sent, use the wp_headers
filter.
function tsg_filter_headers( $headers )
{
// For debug. This will break your page but you will see which headers are sent
// print_r( $headers );
// It’s a good idea to leave the admin alone
if ( !is_admin() ) {
// Add or redefine 'Content-Location' header
$headers['Content-Location'] = '/my-receipts/42';
}
return $headers;
}
add_filter( 'wp_headers', 'tsg_filter_headers' );
See the WordPress doc here.