2
votes

What I'm trying to achieve is to make each user on the website(the wp-users) have their own unique front-end profile pages like so site.com/user/userX. (EDIT: To clarify, it is not a settings page that only the user himself can see, it's a open profile page for logged users or anonymous/visitors to access and see its profile)

I thought about using the url GET (site.com/user?name=userX) in a post but wordpress doesn't seems to understand "?..=.." and says page doesn't exist. This solution does not look like a good practice in wordpress so I'm trying to look from another perspective.

Even tho there are a couple of plugins there that would do that for me and more, I'm stuck with doing manually since I'm building a multi-language website (using Polylang), so plugins will only work in the main language. Briefly, Polylang keeps track of the language through url, ex.: site.com/langX/pageX, and the plugins I've tried are stuck to one page only.

I'm inclined to say what I'm looking for is a way in the functions.php to make Wordpress understand that a page .../user/ will expect a name right after .../user/userX like that I can use the same function for multiple languages : ...langX/user/userX, ...langY/user/userX ...

I'd be glad if someone could put me in the right direction of what should I be looking for.

2
Found the answer in another post, routing : wordpress.stackexchange.com/questions/58683/… - BrunoWB

2 Answers

0
votes

for security you should create a new route in wordpress :

function custom_rewrite_basic() {
  add_rewrite_rule('^user/([0-9]+)/?', 'index.php?user_profile=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');


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

Now, you can access the same page as : http://example.com/user/95

this code in your theme :

get_header(); 
global $wp_query;
if($wp_query->query_vars['user_profile']) {

//do stuff

}
0
votes

From my understanding, what you can try is to create a page from dashboard, named user or something. Once users are logged in, you can get user info in session or you can use the query vars as you mentioned above. You can create custom page templates in Wordpress and assign that template to the page. In this page template write the frontend html-s with required logic. Routing in WordPress is normally handled by WordPress itself. So creating a page will make that route say yoursite.com/user available for you. Please read on page templates from the below link from WordPress documentation.

https://developer.wordpress.org/themes/template-files-section/page-template-files/

UPDATED ANSWER As per the comments

  1. Create a custom post type which holds the data(users)
  2. When a user registers, create the entry for users post type, link user id to that post type entry with a post meta. You can set a unique url for the user by setting the slug as you need.

  3. Provide users the ability to edit only their own data. You can check the post meta which connects user id with that particular custom post type item.

  4. If a user gets removed, delete their custom post type entry as well, so that nothing remains in the DB related to the user.