4

Was trying to create author specific page with author specific URL and it was working fine,but when i created this URL did not taken in to account following 2 factors

  1. Author Name Conflict (similar name).
  2. Paging option to be added to author page (for his published posts)

here is my initial rule

function add_my_rule() { global $wp; $wp->add_query_var('args'); add_rewrite_rule('writer\/(.*)','index.php?pagename=writer&args=$matches[1]','top'); /*global $wp_rewrite; $wp_rewrite->flush_rules();*/ } add_action('init', 'add_my_rule'); 

This was working file for a URL say

www.myblog.com/writer/umesh-awasthi , 

but there can be more author with same name and this will create a issue.So i was planning to have Blog author URL like SO have

www.myblog.com/writer/001/umesh-awasthi where 001 is user id which will always be unique.

second issue is with paging, as i am able to create paging on the author page but when user is clicking second page the URL is coming as

http://localhost/blog/wordpress/writer/umesh-awasthi/page/2/ so as per my URL-Rewrite rule i will get following data as parameter umesh-awasthi/page/2 which means that now my query will not work as it will expect the author name as umesh-awasthi and will get it as umesh-awasthi/page/2

My Question is can i rewrite the rule so that i should get data in following way in three different variables

  1. userid
  2. author name
  3. page number (if it exits) being new to the WP and rewrite i am not sure how i can achieve this thanks in advance

1 Answer 1

8
function add_my_rule() { global $wp; $wp->add_query_var('args'); $wp->add_query_var('arg_username'); add_rewrite_rule('writer/([0-9]+)/([^/]*)/page/([0-9]+)','index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]&paged=$matches[3]','top'); add_rewrite_rule('writer/([0-9]+)/([^/]*)','index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]','top'); /*global $wp_rewrite; $wp_rewrite->flush_rules();*/ } add_action('init', 'add_my_rule'); 

This should do the trick. One rewriterule for writer/user_id/username (username isn't used in the rewriterule but is necessarily to make it work. The second rewriterule is the same except that it adds pagination.


EDIT: added arg_username in code above.

6
  • this is not working as expected, using the above rule giving me only the user-id not writer name, that means for www.myblog.com/writer/001/umesh-awasthi the rule above giving me only 001 and not the writer-name Commented Jan 20, 2012 at 16:44
  • It is working as I said in my answer. If you have the user ID you don't need the username. If you want both then you can add another queryvar (like arg_username) and add it to the rewriterule: add_rewrite_rule('writer/([0-9]+)/([^/]*)','index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]','top'); (also change it in the other reriterule) Commented Jan 20, 2012 at 16:48
  • 1
    may be doing some wrong due to lack of knowledge but add_rewrite_rule('writer/([0-9]+)/([^/]*)','index.php?pagename=writer&args=$matches[1]','top'); add_rewrite_rule('writer/([0-9]+)/([^/]*)','index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]','top'); add_rewrite_rule('writer/([0-9]+)/([^/]*)/page/([0-9]+)','index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]&paged=$matches[3]','top'); rule still not giving me author name. Commented Jan 20, 2012 at 16:57
  • Edited my answer. Commented Jan 20, 2012 at 17:00
  • 2
    My bad, check updated answer (rewriterule for paged had to be on top). Commented Jan 20, 2012 at 17:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.