Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Rewrite API Examples
<IfModule mod_rewrite.c>
# enable rewriting
RewriteEngine on
# don't rewrite files that exist in the file system
RewriteCond %{REQUEST_FILENAME} !-f
# don't rewrite directories that exist in the file system
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the request to index.php
RewriteRule ^ index.php [QSA,L]
</IfModule>
<?php
/**
* Process a requested URI into a simple response
*/
// requested URI
$request = trim( $_SERVER['REQUEST_URI'], '/' );
// default response
$response = '--- not found';
// a empty array in which to parse query strings
$query_vars = [];
// see if the requested uri matches the pattern for a single post
$query = preg_replace_callback(
// the regex pattern to be matched
'#post/(.*)#',
// the callback that fires if the regex is matched
function( $matches ){
// a query-like string with key-value pairs.
return "post=$matches[1]";
},
// the requested URI
$request );
// parse the query string into the array
parse_str( $query, $query_vars );
// respond to the request for a single post
if ( isset( $query_vars['post'] ) ){
$response = "Well hellooooooo! You've reached the post with the slug: {$query_vars['post']}";
}
print $response;
<?php
add_action( 'init', 'debug_endpoint_init' );
add_action( 'loop_start', 'debug_endpoint_loop_start' );
/**
* Add our new debug endpoint
*/
function debug_endpoint_init(){
add_rewrite_endpoint( 'debug', EP_ALL );
}
/**
* Respond to our new endpoint
*/
function debug_endpoint_loop_start(){
// main query only
if ( !is_main_query() ) {
return;
}
$debug = get_query_var( 'debug' );
// look for a debug query variable that has a value
if ( !empty( $debug ) ) {
// show post information if on a permalink
if ( $debug == 'post' ){
$post = get_post();
d( $post );
}
// show wp_query info if debug has value of "query"
if ( $debug == 'query' ) {
global $wp_query;
d( $wp_query );
}
}
}
<?php
add_action( 'init', 'json_endpoint_init' );
add_action( 'template_include', 'json_endpoint_template_include' );
/**
* Add our new json endpoint
*/
function json_endpoint_init(){
add_rewrite_endpoint( 'json', EP_PERMALINK | EP_PAGES );
}
/**
* Respond to our new endpoint
*
* @param $template
*
* @return mixed
*/
function json_endpoint_template_include( $template ){
global $wp_query;
// since the "json" query variable does not require a value, we need to
// check for its existence
if ( is_singular() && isset( $wp_query->query_vars['json'] ) ) {
$post = get_post();
wp_send_json( (array) $post );
}
return $template;
}
<?php
add_action('init', 'rewrite_rule_example');
/**
* Add rewrite rule for a pattern matching "post-by-slug/<post_name>"
*/
function rewrite_rule_example() {
add_rewrite_rule('^post-by-slug/(.*)/?', 'index.php?name=$matches[1]', 'top');
}
<?php
add_action( 'init', 'rewrite_tag_example_init' );
add_action( 'template_redirect', 'rewrite_tag_example_template_redirect' );
/**
* Add rewrite rule and tag to WP
*/
function rewrite_tag_example_init(){
// rewrite tag adds the matches found in the pattern to the global $wp_query
add_rewrite_tag( '%affiliate%', '(.*)' );
}
/**
* Modify the query based on our rewrite tag
*/
function rewrite_tag_example_template_redirect(){
// get the value of our rewrite tag
$affiliate = get_query_var( 'affiliate' );
// check if our rewrite tag has value
if ( !empty( $affiliate ) ){
// track where this visitor came from
setcookie( 'affiliate', $affiliate, 0, '/' );
}
}
<?php
add_action( 'init', 'url_longerer_init' );
add_action( 'template_redirect', 'url_longerer_template_redirect' );
/**
* Add rewrite rule and tag to WP
*/
function url_longerer_init(){
// rewrite rule tells wordpress to expect the given url pattern
add_rewrite_rule( '^longerer/(.*)/?', 'index.php?longerer=$matches[1]', 'top' );
// rewrite tag adds the matches found in the pattern to the global $wp_query
add_rewrite_tag( '%longerer%', '(.*)' );
}
/**
* Modify the query based on our rewrite tag
*/
function url_longerer_template_redirect(){
// get the value of our rewrite tag
$longerer = get_query_var( 'longerer' );
// look for the existence of our rewrite tag
if ( get_query_var( 'longerer' ) ){
// get the post ID from the longerer string
$post_ID = url_longerer_decode_in_some_way( $longerer );
// attempt to find the permalink associated with this post ID
$permalink = get_permalink( $post_ID );
// if valid, send to permalink
if ( $post_ID && $permalink ){
wp_redirect( $permalink );
}
// otherwise, send to homepage
else {
wp_redirect( home_url() );
}
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment