1

This may be something simple for most PHP coders, however, I have never had to do this before. I am using Wordpress website with a Page Template. On this page is a page I created to Query results by User_id, then displaying the users profile information. What I am trying to achieve is the following:

Page #1 Lawyers Directory > Has Multiple Lawyers being pulled from Database

Action: Click Link (Example Link: http://example.com/lawyer-profile?user_id=9) *the number 9 is the dynamic number

Page #2 Lawyer Profile > Has Profile Info of Lawyer

PROBLEM How do I change this URL http://example.com/lawyer-profile?user_id=9

TO

http://example.com/lawyer-profile/john-doe/

But still pull the data by using the User_id?

Any help would be appreciated!


UPDATE: I found this on wordpress's website and I modified it to reflect what I would need it to do but it doesn't work. It redirects to the lawyer-directory page which is page_id 189 but still shows the query strings in the URL.

Here is the url i am using: http://example.com/index.php?page_id=189&userid=195&fullname=jack-bowers

and it should look like this: http://example.com/lawyer-directory/jack-bowers/

any suggestions?

 function custom_rewrite_tag() {
   add_rewrite_tag('%userid%', '([^&]+)');
   add_rewrite_tag('%fullname%', '([^&]+)');
 }
 add_action('init', 'custom_rewrite_tag', 10, 0);

 function custom_rewrite_rule() {
     add_rewrite_rule('^lawyer-directory/([^/]*)/([^/]*)/?','index.php?page_id=189&userid=$matches[1]&fullname=$matches[2]','top');
 }
 add_action('init', 'custom_rewrite_rule', 10, 0);
1
  • 2
    “But still pull the data by using the User_id?” – if you are not passing the id as a parameter in your URL, then you can not “pull the data” by that id, because you do not have the id in your script … that should be pretty obvious. Either pass the id as well (many systems use something like /lawyer-profile/9/john-doe/, where only the 9 has actual meaning and the rest doesn’t matter) – or rewrite your script to select the data by the value that does get passed instead, john-doe. For that to properly work, the value should be unique within your data set of course. Commented Jan 27, 2015 at 12:27

1 Answer 1

1

If you're using Wordpress, take a look at add_rewrite_rule(). Along with add_rewrite_tag, this will allow you to handle that.

What you can do is define a rewrite tag for user_id, and a rewrite rule to map your pattern to the single page that handles it. You can then get the value using query_vars eg.

$wp_query->query_vars['user_id']

So this would return eg. john-doe. On your database you would have an index for the lawyer's permalink which you can then look up to find their id/record.

It's a bit confusing at first, but there is a decent example in the doc linked above.

Sign up to request clarification or add additional context in comments.

1 Comment

I posted an update with a code snippet. Can you take a look at it for me and tell me why it isn't working? It will go to the correct page, but doesn't show the url like it's suppose to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.