How to Create a Custom Plugin Permalink

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.

Read More

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

}

Related posts

Leave a Reply

2 comments

  1. 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.

  2. 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();
    
    }