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 https://example.com/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.

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? – simongcc Apr 17 '20 at 7:19
  • I am using it inside template_redirect hook. on plugin activation i called rewriteURL() which have all the hooks required. – DevD Apr 17 '20 at 7:40
0

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';
        });
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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