Reputation: 199
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);
Upvotes: 1
Views: 374
Reputation: 2905
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.
Upvotes: 1