最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rewrite rules - Custom url for a plugin page add_rewrite_rule Wordpress

programmeradmin1浏览0评论

I am trying to create a custom url which when hits, loads the plugin file as a template file. On this plugin page i will show progress bar with status of zip being generating.

Here is my plugin code.

register_activation_hook( __FILE__, array( $this, 'plugin_activation_code' ) );
public function plugin_activation_code(){
    $this->zip_progress_rule();
    $this->rewriteURL();
}

public function zip_progress_rule(){
    //add_rewrite_rule('^zip-backup/([^/]*)/([^/]*)/?',PLUGIN_DIRNAME.'/includes/progress.php?backup=$matches[1]&key=$matches[2]','top');
    //add_rewrite_rule('^zip-backup/(.+)/?$',PLUGIN_DIR_URL.'includes/progress.php?backup=$matches[1]&key=$matches[2]','top');

    add_rewrite_rule(
        '^zip-backup/([^/]*)/([^/]*)/?',
        'index.php?backup=$matches[1]&key=$matches[2]',
        'top'
    );
}

public function rewriteURL(){
    add_action('init', array($this, 'progress_tags'), 10, 0);
    add_action('template_redirect',  array($this, 'custom_template_redirect'));
    self::flush_rewrite_rules();
}

public function progress_tags(){
    add_rewrite_tag('%backup%', '([^&]+)');
    add_rewrite_tag('%key%', '([^&]+)');
}

public function custom_template_redirect() {
    if(get_query_var('backup')) {
        add_filter('template_include', function() {
            return PLUGIN_DIR_PATH . 'includes/progress.php';
        });
    }
}

public static function flush_rewrite_rules() {
    error_log('Flush rewrite rule');
    global $wp_rewrite;
    $wp_rewrite->flush_rules(true);
}

The url will look like . But each time i hit this url, it gives 404 page.

I found a similar post here and implemented its code. But i am still getting 404 page.

Can anyone help me where i am doing wrong.

Thanks in advance.

I am trying to create a custom url which when hits, loads the plugin file as a template file. On this plugin page i will show progress bar with status of zip being generating.

Here is my plugin code.

register_activation_hook( __FILE__, array( $this, 'plugin_activation_code' ) );
public function plugin_activation_code(){
    $this->zip_progress_rule();
    $this->rewriteURL();
}

public function zip_progress_rule(){
    //add_rewrite_rule('^zip-backup/([^/]*)/([^/]*)/?',PLUGIN_DIRNAME.'/includes/progress.php?backup=$matches[1]&key=$matches[2]','top');
    //add_rewrite_rule('^zip-backup/(.+)/?$',PLUGIN_DIR_URL.'includes/progress.php?backup=$matches[1]&key=$matches[2]','top');

    add_rewrite_rule(
        '^zip-backup/([^/]*)/([^/]*)/?',
        'index.php?backup=$matches[1]&key=$matches[2]',
        'top'
    );
}

public function rewriteURL(){
    add_action('init', array($this, 'progress_tags'), 10, 0);
    add_action('template_redirect',  array($this, 'custom_template_redirect'));
    self::flush_rewrite_rules();
}

public function progress_tags(){
    add_rewrite_tag('%backup%', '([^&]+)');
    add_rewrite_tag('%key%', '([^&]+)');
}

public function custom_template_redirect() {
    if(get_query_var('backup')) {
        add_filter('template_include', function() {
            return PLUGIN_DIR_PATH . 'includes/progress.php';
        });
    }
}

public static function flush_rewrite_rules() {
    error_log('Flush rewrite rule');
    global $wp_rewrite;
    $wp_rewrite->flush_rules(true);
}

The url will look like https://example/zip-backup/c3Rhcn245Q/so7KpjWf234dfgrrTEHU233. But each time i hit this url, it gives 404 page.

I found a similar post here and implemented its code. But i am still getting 404 page.

Can anyone help me where i am doing wrong.

Thanks in advance.

Share Improve this question asked Apr 17, 2020 at 7:09 DevDDevD 1314 bronze badges 2
  • why you put the template_include filter inside another function? what is the running sequence of custom_template_redirect, what hook do you use on it? – 西門 正 Code Guy - JingCodeGuy Commented Apr 17, 2020 at 7:19
  • I am using it inside template_redirect hook. on plugin activation i called rewriteURL() which have all the hooks required. – DevD Commented Apr 17, 2020 at 7:40
Add a comment  | 

1 Answer 1

Reset to default 2

After a long search finally i found a working solution. I am posting here in case someone face the similar issue, it would be helpful. Here is my working code:

add_action('template_redirect',  array($this, 'zip_template'));         
add_filter( 'rewrite_rules_array', array($this, 'zip_rule') );
add_filter( 'query_vars', array( $this, 'zip_query_vars' ) );
add_action( 'wp_loaded', array( $this, 'flush_rules' ) );

/**
* Rewrite rule
**/

public function zip_rule($rules ) {
    $newrules = array();
    $newrules['zip-backup/([^&]+)/([^&]+)'] = 'index.php?backup=$matches[1]&key=$matches[2]';
    return $newrules + $rules;
}

/**
* query parameters
**/

public function zip_query_vars($vars) {
    $vars[] = 'backup';
    $vars[] = 'key';
    return $vars;
}

/**
* Flush rules
**/

public function flush_rules(){
    $rules = get_option( 'rewrite_rules' );

    if ( ! isset( $rules['zip-backup/([^&]+)/([^&]+)'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

/**
* Callback template
**/

public function zip_template() {
    if(get_query_var('backup')) {
        add_filter('template_include', function() {
            return PLUGIN_DIR_PATH . 'zip-backup.php';
        });
    }
}
发布评论

评论列表(0)

  1. 暂无评论