A fairly simple way to do this is to create a new page with the slug likedby
, then add some rewrite rules to handle the extra parameters.
First, the query var that holds the requested username:
function wpd_likedby_query_var( $query_vars ) {
$query_vars[] = 'likeduser';
return $query_vars;
}
add_filter( 'query_vars', 'wpd_likedby_query_var' );
Then a couple of rewrite rules to handle the requests, directing them to our likedby
page and setting the extra parameters:
function wpd_likedby_rules(){
add_rewrite_rule(
'likedby/([^/]+)/page/?([0-9]{1,})/?$',
'index.php?pagename=likedby&likeduser=$matches[1]&paged=$matches[2]',
'top'
);
add_rewrite_rule(
'likedby/([^/]+)/?$',
'index.php?pagename=likedby&likeduser=$matches[1]',
'top'
);
}
add_action( 'init', 'wpd_likedby_rules' );
You can then create a page-likedby.php
template and put your custom queries in there. Use get_query_var( 'likeduser' )
and get_query_var( 'paged' )
to get the query parameters.
I also recommend looking at wpdb::prepare()
for generating queries that accept unknown input.