如何创建自定义插件固定链接

时间:2012-01-15 作者:Brooke.

我写了一个simple plugin 这将在某个日期将用户重定向到某个页面。(在这种情况下,是为了在美国针对SOPA的管制)

现在我在用电话wp_redirect(plugins_url( \'simple-sopa-blackout/blackout.php\'),302 ); 在给定日期重定向用户。

问题是它会让一个讨厌的URLhttp://example.net/wp-content/plugins/simple-blackout/blackout.php 如果能做到这一点就太好了http://example.net/blackout

我遵循我的想法the proper codex 我试过这个:

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;
}
任何帮助都会很棒,完整的来源是here

编辑:

这是我现在所拥有的,如果使用查询变量访问页面,它就会起作用index.php?blackout=stop-sopa 但如果我去blackout/stop-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
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();

}

2 个回复
最合适的回答,由SO网友:Jared 整理而成

我想这个答案可以帮助你。在插件中创建一个自定义页面模板,然后使用该模板创建一个名为Blackout的页面,并将插件中的用户重定向到该页面。

帮助来源:Create custom page templates with plugins?

Edit with code sample:

假设你有一个叫Blackout的页面,slug是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;
}
只要您能够让重定向在应该的时候正常工作,上面的代码应该与它一起工作,以便在访问该页面时显示页面模板。现在您可以自定义your-custom-page-template.php 或者从插件文件夹中命名,而不是从主题中命名。

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\' );
在“二十一”主题上,只有几个其他插件处于活动状态。

SO网友:Bainternet

试试这个:(url应该是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();

}

结束

相关推荐

将目录路径传递给plugins_url()安全吗?

plugins_url() 函数接受插件slug或文件路径来构建URL。我有以下目录结构:/wp-content/mu-plugins/someplugin/css/file.css /wp-content/mu-plugins/someplugin/includes/file.php 我需要建立URL到file.css 在里面file.php. 我不能通过__FILE__ 因为这将是一个层次太深。plugins_url(\'css/file.css\', __FILE__ )