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 |1 Answer
Reset to default 2After 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';
});
}
}
template_include
filter inside another function? what is the running sequence ofcustom_template_redirect
, what hook do you use on it? – 西門 正 Code Guy - JingCodeGuy Commented Apr 17, 2020 at 7:19template_redirect
hook. on plugin activation i calledrewriteURL()
which have all the hooks required. – DevD Commented Apr 17, 2020 at 7:40