I wrote a simple plugin that will redirect the users to a page on a certain date. (in this case it’s for the Blackout against SOPA here in the US)
Right now I’m using the line wp_redirect(plugins_url( 'simple-sopa-blackout/blackout.php'),302 );
to redirect the users on the given date.
The problem is it makes a nasty URL like http://example.net/wp-content/plugins/simple-blackout/blackout.php
It would be great to just make this http://example.net/blackout
I Following what I thought was the proper codex I tried this:
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['blackout/?$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['blackout/? $'] = plugins_url( 'simple-sopa-blackout/blackout.php');
return $newrules + $rules;
}
Any help would be awesome, the full source is here
EDIT:
Here’s what I have now, it works if access the page with the query vars index.php?blackout=stop-sopa
but not if i go to blackout/stop-sopa
it also seems to be creating a loop and timing out if the date is true
//create your rewrite rule
add_action( 'init', 'wpse38978_init' );
function wpse38978_init() {
add_rewrite_rule('blackout/(d*)$', 'index.php?blackout=$matches[1]', 'top');
}
// add blackout to the whitelist of variables it wordpress knows and allows
function my_plugin_query_vars($query_vars) {
$query_vars[] = 'blackout';
return $query_vars;
}
add_filter('query_vars', 'my_plugin_query_vars');
function my_plugin_parse_request($wp) {
if (array_key_exists('blackout', $wp->query_vars)
&& $wp->query_vars['blackout'] == 'stop-sopa') {
include( dirname( __FILE__ ) . '/blackout.php' );
exit();
}
}
add_action('parse_request', 'my_plugin_parse_request');
if(!function_exists('wp_redirect')) {
require(ABSPATH . WPINC . '/pluggable.php');
}
$current_time = current_time('mysql', '0'); //get current blog time
$ts =strtotime($current_time); //parse the sql blog time to a php useable format
$check_date = date('m/d/Y', $ts); // put the date in a format we can check
$check_hour = date('H', $ts); // put the date in a format we can check
$blackout_day = "01/15/2012"; // should we black out the site?
$blackout_day_time_start = "08"; // when should we start (hour in 24 hour format)
$blackout_day_time_end = "20"; // should we black out the site?
if((!is_admin()) && ($check_date == $blackout_day && ($check_hour >= $blackout_day_time_start || $check_hour < $blackout_day_time_end))){
wp_redirect(get_bloginfo('url').'/blackout/stop-sopa',302 );
exit();
}
I think this answer could help you. Make a custom page template within your plugin and then create a page with that template called Blackout, and redirect users in the plugin to that page.
Help from: Create custom page templates with plugins?
Edit with code sample:
Say you have a page called Blackout, the slug is
blackout
.As long as you can get the redirect to work properly when it should, this code above should work in conjunction with it to display the page template whenever that page is visited. Now you can customize
your-custom-page-template.php
or whatever you name it from within your plugins folder instead of the theme.On the Twenty Eleven theme, only a couple other plugins active.
try this : ( the url should be http://site.com/blackout/anti-sopa)