Last active
January 18, 2023 19:29
-
-
Save daggerhart/5be1ab9a3300353033fc7a55eedcd4e5 to your computer and use it in GitHub Desktop.
WordPress Rewrite API Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, '/' ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'init', 'rewrite_tag_permalink_init' ); | |
add_action( 'pre_get_posts', 'rewrite_tag_permalink_pre_get_posts' ); | |
// normal posts | |
add_filter( 'post_link', 'rewrite_tag_permalink_post_link', 10, 2 ); | |
// custom post types | |
add_filter( 'post_type_link', 'rewrite_tag_permalink_post_link', 10, 2 ); | |
/** | |
* Add our rewrite rule and rewrite tag | |
*/ | |
function rewrite_tag_permalink_init(){ | |
// rewrite rule looks for custom_folder | |
add_rewrite_rule( '^([^/]+)/(.*)/?', 'index.php?custom_folder=$matches[1]&name=$matches[2]', 'bottom' ); | |
// rewrite tag puts custom_folder value into the query vars | |
add_rewrite_tag( '%custom_folder%', '(.*)' ); | |
} | |
/** | |
* Alter permalinks to reflect the new custom_folder rewrite tag | |
* | |
* @param $permalink | |
* @param $post | |
* | |
* @return mixed | |
*/ | |
function rewrite_tag_permalink_post_link( $permalink, $post ) { | |
$rewrite_tag = '%custom_folder%'; | |
// return early if tag not found | |
if ( strpos( $permalink, $rewrite_tag ) === FALSE ) { | |
return $permalink; | |
} | |
// look for custom permalink meta data | |
$custom_folder = get_post_meta( $post->ID, 'custom_folder', true ); | |
// if a custom permalink exists as meta data, use it | |
if ( !empty( $custom_folder ) ){ | |
$permalink = str_replace( $rewrite_tag, $custom_folder, $permalink ); | |
} | |
// fallback by completely removing our tag from the permalink | |
else { | |
$permalink = str_replace( $rewrite_tag . '/', '', $permalink ); | |
} | |
return $permalink; | |
} | |
/** | |
* Use pre_get_posts to make the custom_folder meta data value optional | |
* | |
* @param $query | |
*/ | |
function rewrite_tag_permalink_pre_get_posts( $query ){ | |
if ( $query->is_main_query() ){ | |
// if only the first argument is presented, use it as the post name | |
if ( get_query_var( 'custom_folder' ) && | |
!get_query_var( 'name' ) ) | |
{ | |
// what we thought was the custom_folder is actually the post_name | |
$query->set( 'name', get_query_var( 'custom_folder' ) ); | |
$query->is_home = false; | |
$query->is_singular = true; | |
$query->is_single = true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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