Wordpress 之 Rewrite Rules
2024-09-06 17:30:49
-----------------------------------------------------------------
场景描述:
自定义wp主题中,添加了个关于页面(about.php) 。
目的:
顺利访问 http://<domain>/about.php
-----------------------------------------------------------------
解决方案:在主题的functions.php文件中, 自定义重写规则 rewrite_rules
-----------------------------------------------------------------
Step 1. 添加 generate_rewrite_rules
if ( ! function_exists( 'testthemes_rewrite_rules' ) ) :
/**
* @param WP_Rewrite $wp_rewrite
*/
function testthemes_rewrite_rules( $wp_rewrite ) {
$testthemes_rules = [
'about(.*)$' => 'index.php?my_custom_page=about',
]; $wp_rewrite->rules = $testthemes_rules + $wp_rewrite->rules;
}
endif; // testthemes_rewrite_rules add_action( 'generate_rewrite_rules', 'testthemes_rewrite_rules' );
说明:
pattern=>url格式: 'about(.*)$' => 'index.php?my_custom_page=about'
-----------------------------------------------------------------
Step 2. 添加 query_vars
if ( ! function_exists( 'testthemes_add_query_vars' ) ) :
/**
* @param array $public_query_vars
* @return array
*/
function testthemes_add_query_vars($public_query_vars) {
$public_query_vars[] = 'my_custom_page';
return $public_query_vars;
}
endif;
add_action( 'query_vars', 'testthemes_add_query_vars' );
-----------------------------------------------------------------
Step3. 添加 template_redirect
if ( ! function_exists( 'testthemes_template_redirect' ) ) :
/**
* @void
*/
function testthemes_template_redirect() {
global $wp;
/**@var WP_Query $wp_query*/
global $wp_query;
/**@var WP_Rewrite $wp_rewrite*/
global $wp_rewrite; //查询my_custom_page变量
$my_custom_page = $wp_query->query_vars['my_custom_page'];
switch ($my_custom_page) {
case 'about':
include(TEMPLATEPATH.'/about.php');
die();
}
}
endif;
add_action( 'template_redirect', 'testthemes_template_redirect' );
-----------------------------------------------------------------
Step4. 添加 load-themes.php
if ( ! function_exists('testthemes_flush_rewrite_rules')):
/**
* @void
*/
function testthemes_flush_rewrite_rules(){
/**@var string $pagenow*/
global $pagenow;
/**@var WP_Rewrite $wp_rewrite*/
global $wp_rewrite; if( 'theme.php' == $pagenow && isset( $_GET['activated'] )) {
$wp_rewrite->flush_rules();
}
}
endif;
add_action( 'load-themes.php', 'testthemes_flush_rewrite_rules' );
-----------------------------------------------------------------
Step5. 重新激活主题
END
最新文章
- tomcat配置性能调优1----server.xml文件详解
- JavaBean与Jsp
- p4-hlir/test源码 stateful.p4 control_flow_opt.p4
- 使用AndroidStudio编译NDK的方法及错误解决方案
- LintCode ";Maximum Gap";
- PHP实现站点pv,uv统计(二)
- EIGRP默认路由分发的四种方法
- DIH中添加不同的数据源
- 自定义HBase的协处理器(Observer)
- C# 线程--第四线程实例
- CocoaPods 安装和使用
- 为什么我的outlook只能收信不能发信,发送测试电子邮件消息: 无法发送此邮件。请在帐户属性中验证电子邮件
- Java 中值传递和引用传递 区分
- deepin系统下如何设置wifi热点(亲测有效)
- LCA—倍增法求解
- 【安卓开发】Layout Inflation不能这么用
- SYM File
- 51-迷宫(一)- java版dfs和bfs
- 使用机器学习检测TLS 恶意加密流——业界调研***有开源的数据集,包括恶意证书的,以及恶意tls pcap报文***
- WikiBooks/Cg Programming
热门文章
- tensorflow sigmoid_cross_entropy_with_logits 函数解释
- BZOJ_4809_皇后_爆搜
- CF 757E Bash Plays with Functions——积性函数+dp+质因数分解
- 实用的MVVM:ImageView
- Linux查看CPU《型号..》《内存..》《硬盘..》《系统..》
- 【旧文章搬运】KeUserModeCallback用法详解
- Python深入浅出property特性属性
- Numbers Exchange
- Several ports (8005, 8080, 8009) required
- 《剑指offer》面试题7—用两个栈实现队列