我尝试short url
中my-pugin
的自定义页面
当我使用自定义页面时,我的网址会像波纹管一样
http://localhost/wordpress/wp-content/plugins/my-plugin/testurl.php
但是我想要这个
http://localhost/wordpress/testurl
我将此代码写在我的插件中
function create_rewrite_rules($rules) {
//print_r($rules);
global $wp_rewrite;
$newRule = array('/testurl' => 'wp-content/plugins/my-plugin/testurl.php');
//echo $newRule;
$newRules = $newRule + $rules;
//$newRules = $newRule;
// echo "<pre>";
// print_r($newRules);
return $newRules;
}
function flush_rewrite_rules() {
//echo 1;
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
if(class_exists('MyPlugin'))
$myPlugin = new MyPlugin();
add_filter('rewrite_rules_array', array($myPlugin, 'create_rewrite_rules'));
add_filter('init', array($myPlugin, 'flush_rewrite_rules'));
但它无济于事。
答案 0 :(得分:1)
您需要注意一些事项:
rewrite_rule
第一个参数必须为正则表达式。init
钩子是一个动作,而不是过滤器。以下插件代码确实有效:
<?php
/*
Plugin Name: Rewrite test
*/
function custom_rewrite_basic() {
add_rewrite_rule('^testurl?', 'wp-content/plugins/my-plugin/testurl.php', 'top');
}
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'myplugin_flush_rewrites' );
function myplugin_flush_rewrites() {
custom_rewrite_basic();
flush_rewrite_rules();
}
希望这会有所帮助。