Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();

}
share|improve this question
    
Why don't create Blackout page and then redirect use to that page? –  Mamaduka Jan 15 '12 at 9:44
    
@Mamaduka I support that idea just don't know how to do that from a plugin. –  BandonRandon Jan 15 '12 at 19:03
    
tech.ipstenu.org/2012/sopa-blackout here's great idea by ipstenu wiht .maintenance maybe you can find it useful –  Mamaduka Jan 15 '12 at 19:42
add comment

2 Answers

up vote 1 down vote accepted

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.

add_filter( 'page_template', 'your_custom_page_template' );
function your_custom_page_template( $page_template ) {

    if ( is_page( 'blackout' ) ) {
        $page_template = plugin_dir_path( __FILE__ ) . 'your-custom-page-template.php';
    }

    return $page_template;
}

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.

Edit, I got this working:

function blackout_redirect(){
    $page = get_page_by_path( 'blackout' );

    if( isset( $page ) && !is_null( $page ) ) {
        if( !is_page( 'blackout' ) && !is_admin() /* && $check_date stuff */ ) {
            wp_redirect( get_permalink( $page->ID ), 302 ); exit;   
        }
    }
}
add_action( 'wp', 'blackout_redirect' );

function blackout_template() {

    if( is_page( 'blackout' ) ) {
        include plugin_dir_path( __FILE__ ) . 'blackout.php'; exit;
    }

}
add_filter( 'template_redirect', 'blackout_template' );

On the Twenty Eleven theme, only a couple other plugins active.

share|improve this answer
    
That should work great, now if i can figure out how to make a custom page template from within the plugin :) –  BandonRandon Jan 16 '12 at 7:54
    
The link I posted should give you the solution there on making the custom page template from within a plugin. I will update my answer with code samples. –  Jared Jan 16 '12 at 9:07
    
I tried that but i can't access example.com/blackout it returns a 404. Also if i'm redirecting to a page it seems to create a redirect loop. –  BandonRandon Jan 16 '12 at 20:10
    
@BandonRandon Also make sure you don't run the redirect when they are on the Blackout page, otherwise it will keep redirecting itself to the same page over and over resulting in a redirect loop. I'm writing a plugin to see if I can reproduce the problem or maybe come up with a solution. –  Jared Jan 18 '12 at 11:30
    
@BandonRandon can you give my most recent edit a try? It works on my end just without the date logic. –  Jared Jan 18 '12 at 11:30
show 1 more comment

try this : ( the url should be http://site.com/blackout/anti-sopa)

//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
add_action( 'query_vars', 'wpse6891_query_vars' );
function wpse38978_query_vars( $query_vars )
{
    $query_vars[] = 'blackout';
    return $query_vars;
}

// If this is done, we can access it later
// This example checks very early in the process:
// if the variable is set, we include our page and stop execution after it
add_action( 'parse_request', 'wpse38978_parse_request' );
function wpse6891_parse_request( &$wp ){
    if ( array_key_exists( 'blackout', $wp->query_vars ) &&  $wp->query_vars['blackout'] == 'anti-sopa') {
        include( dirname( __FILE__ ) . '/blackout.php' );
        exit();
    }
}


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/18/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/anti-sopa',302 );
      exit();

}
share|improve this answer
    
that is partly working, see edit, also query_vars is a filter not action :) –  BandonRandon Jan 15 '12 at 19:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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