我试图创建一个自定义url,当点击时,将插件文件加载为模板文件。在这个插件页面上,我将显示状态为zip正在生成的进度条。
这是我的插件代码。
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);
}
url如下所示https://example.com/zip-backup/c3Rhcn245Q/so7KpjWf234dfgrrTEHU233
. 但每次我点击这个url,它就会显示404页。我找到了一个类似的帖子here 并实现了其代码。但我仍然得到404页。
谁能帮我找出哪里做错了。
提前谢谢。