Rewrite API Class
The global rewrite API instance is stored under $wp_rewrite
The rewrite class is located under wp-includes/class-wp-rewrite.php
This class is responsible for generating and executing the rewrite rules in WordPress. The request we get on our WordPress page is parsed with the WP & WP_Rewrite class to generate the query variables and execute the query on the basis of the current request.
Properties and methods in the rewrite class
- $permalink_structure -> This is the permalink structure alloted for the posts.
- $use_trailing_slashes -> Should a slash be added after the whole link [ / at the end ]
- $author_base -> Base for the permalink structure for the author archive page, changing this value by [ $wp_rewrite->author_base ] would change the base for the author archive page. Base is like front.
- $author_structure -> The generated author archive page structure [ permalink for author archive ]
- $date_structure -> The generated date archive page structure [ permalink for date archive ]
- $page_structure –> Permalink structure for the page post type.
- $search_base -> Base for the permalink structure for the search results page, changing this value by [ $wp_rewrite->search_base ] would change the base for the search result page.
- $search_structure -> The generated search result page structure [ permalink for search page ]
- $comments_base -> Base for comments, mostly used while the comments pagination is happening
- $pagination_base -> Default pagination base, [ used in different location ( home, archives etc ) ]
- $comments_pagination_base -> Base for comments pagination
- $feed_base -> Base for feed links -> rss etc etc feeds.
- $comment_feed_structure -> Permalink structure generated for comment feeds page.
- $feed_structure -> Permalink generated for the feed page
- $front -> Front is some static string that is added to the start of posts permalink structure. [ https://mysite.local/test/%post_name%, the front here is test ]
- $index -> This would be the file which is the entry point for all request [ the data from all the requests would be sent to the file defined here ]
- $rules -> Generated rewrite rules are stored here
- $extra_rules -> The extra rules added dynamically are stored here [ the ones from add_rewrite_rule() ]
- $extra_rules_top -> The extra rules which must be added to the top are stored in here [ the ones generated from add_rewrite_rule() with the ]
- $non_wp_rules -> The non WordPress rules which need to be entered to some other file except the $index, these can be added via add_external_rule() -> these would be written to index.php
- $extra_permastructs -> Extra permalink structured added via add_permastruct() are stored here, for e.g. for custom post types
- $endpoints -> Endpoints added via add_rewrite_endpoint() are stored here
- $use_verbose_rules -> Set true if all the WordPress related rules must be added to .htaccess
- $rewritecode -> The default supported rewrite codes are added here
- $rewritereplace -> The regex expressions for the above rewrite codes are added here
- $queryreplace -> The query_vars for the above rewrite codes are added here
How do we override the permalink structures programmatically?
WordPress uses functions like get_date_permastruct, get_year_permastruct, get_month_permastruct, get_page_permastruct, etc etc to get the permalinks, now if we alter the values for the $page_structure via the global rewrite object, the get_page_permastruct would return the value we have given to the variable, in this way we can alter it.
function modify_permalink_structures() {
global $wp_rewrite;
$wp_rewrite->page_structure = 'test-pages/%pagename%';
}
add_action( 'init', 'modify_permalink_structures', 11 );Adding/Removing permalink structures?
For adding:
WordPress provides us with the function add_permastruct()
This function internally calls the add_permastruct() from the global rewrite variable which just internally adds our permastructure to the $extra_permastructs variable.
For removing:
WordPress provides us with the function remove_permastruct() which same as the add function internally calls the remove_permastruct() function from the global rewrite object, and that function internally just removes the permalink name from the $extra_permastructs array
Changing the permalink structure for a custom post type
Our post type here : mlb_movie
function add_movie_rewrite_rules() {
global $wp_rewrite;
$args = $wp_rewrite->extra_permastructs['mlb_movie']; // fetching the previous details
remove_permastruct( 'mlb_movie' ); // deleting the previous details
unset( $args['struct'] ); // removing the previous structure from the array
add_permastruct( 'mlb_movie', '%genre%/%mlb_movie%', $args ); // adding the new structure
}
add_action( 'init', 'add_movie_rewrite_rules', 11 );Flushing Rewrite Rules
Flushing of the rewrite rules is necessary after you have changed them, as the rewrite rules are stored and used from the database, to do that we can simply call the function
flush_rewrite_rules(); // flush the rewrite rulesThis function internally calls the flush_rules() from the rewrite class.
The function flush_rules() internally deletes all the data inside the rewrite_rules row in wp_options table and then calls the function wp_rewrite_rules(), which generates the new rewrite rules and updates the database with the newly added rules.
