Last active
July 3, 2016 21:58
-
-
Save xeiter/040ccab2971943b72a6729a05ec763ee to your computer and use it in GitHub Desktop.
Wordpress - add custom rewrite rule
This file contains hidden or 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 | |
// Create the query var so that WP catches your custom /staff/username url | |
add_filter( 'query_vars', 'bb_rewrite_add_var' ); | |
function bb_rewrite_add_var( $vars ) | |
{ | |
$vars[] = 'staff'; | |
return $vars; | |
} | |
add_rewrite_tag( '%staff%', '([^&]+)' ); | |
add_rewrite_rule( | |
'^staff/([^/]*)/?', | |
'index.php?staff=$matches[1]', | |
'top' | |
); | |
add_action( 'template_redirect', 'bb_rewrite_catch' ); | |
function bb_rewrite_catch() | |
{ | |
global $wp_query; | |
if ( array_key_exists( 'staff', $wp_query->query_vars ) ) { | |
include (TEMPLATEPATH . '/user-profile.php'); | |
exit; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment