I want to create a public front-end profile page with a friendly url like this format.
mysite.com/user/someusername
Any idea on how can i achieve this? I know it has something to do with rewrite rule but i have no idea on how can i do that. If you got any link or tutorial for me that will be great.
Thanks!
There are two ways I’ve discovered doing this:
The first is more simple to implement, but may not work in all circumstances (one of which I’ll describe soon).
Custom Rewrite Rule
I found this solution a few days ago here: URL Rewriting
And here’s the code, with comments:
You can then use the built in author.php template, modifying to your hearts content.
Sincerely, check the link listed above, as Jan Fabry does an excellent job explaining everything.
Query Vars and Page Templates
For the theme I was working on while discovering these solutions, I needed to serve a custom page based on a user meta value (a separate ID). My client did not want the username or user ID visible publicly, so we created a separate layer.
The only problem? At the moment, there isn’t a clear way how to use the Rewrite API to query by meta keys/values. Luckily, there was a solution.
In your functions.php file…
And then, you need to create a new rewrite tag and rule so that it knows when and how to handle the new query var.
Once you’ve done this, you merely need to “catch” when the query var is being served, and then redirect to the template of your choice:
Just make sure you have created user-profile.php.
In my own example, I created a third function that matched the “public User ID” to the actual user_id via the $wpdb->usermeta table, and passed the information to the template.
If you need to create a template different from the rest of your theme, remember that with get_header, you can specify a name:
Which will call the header-user.php file.
Conclusion
Both of these are valid, working solutions. The second offers a separate layer of “security” as it does not reveal user IDs or usernames, if other people will be able to browse the profiles.
Hope that helps, let me know if you have any questions.
I found this earlier today and made some modificatios to @bybloggers code with the diference that instead of using template_redirect I change the request to show a static page, that means that you can now add anything you like to a page template and use it on that page.
Spanish -> usuario = user <- English
This is my working code based on @bybloggers answer (thanks by the way)
I just learn that we shouldn’t be using template_redirect hook and then exiting php, because some things may stop working because the abrupt interruption of php executing code.
The explanation is here: https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/
So, we should be using the template_include hook. With this hook, there is no need to use the redirect and exit method.
And the other difference is that I just needed http://www.server.com/myaccount without the /userid.
This is the code:
Another way is to use Buddypress which have this built-in even if the plugin have even more functionality which may not be of interest.
http://wordpress.org/extend/plugins/buddypress/