Skip to content

Instantly share code, notes, and snippets.

@xeiter
Last active July 3, 2016 21:58
Show Gist options
  • Save xeiter/040ccab2971943b72a6729a05ec763ee to your computer and use it in GitHub Desktop.
Save xeiter/040ccab2971943b72a6729a05ec763ee to your computer and use it in GitHub Desktop.
Wordpress - add custom rewrite rule
<?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