zoukankan      html  css  js  c++  java
  • Wordpress 之 Rewrite Rules

    -----------------------------------------------------------------

    场景描述:

           自定义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

  • 相关阅读:
    LeetCode(111) Minimum Depth of Binary Tree
    LeetCode(108) Convert Sorted Array to Binary Search Tree
    LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode(99) Recover Binary Search Tree
    【Android】通过经纬度查询城市信息
    【Android】自定义View
    【OpenStack Cinder】Cinder安装时遇到的一些坑
    【积淀】半夜突然有点想法
    【Android】 HttpClient 发送REST请求
  • 原文地址:https://www.cnblogs.com/gouge/p/7159580.html
Copyright © 2011-2022 走看看