A
A
alekamisleforma2020-06-11 04:28:21
WordPress
alekamisleforma, 2020-06-11 04:28:21

How to make a user profile on Wordpress and remove the redirect to the main page?

Hello. PHP, like Wordpress, I still don’t really know, but there is a desire to figure it out. I need to add a user profile so that any other person can view this profile.

I created a profile page that displays user login data and allows you to change some of this data. The link to the page looks like this localhost/profile . And I can't figure out how I can do localhost/profile/username like this . The logic, probably, should be like this .. when you go to the profile page, if there is something after "profile", then the script searches the database for a user with such a login, and if it exists, it displays data, and if not, then something then type "User not found".

So, how can I make it so that when I click on a link of the formlocalhost/profile/username , I stayed on the profile page and ran the script for that page. Because now wordpress detects the transition to the specified link as a 404 error and redirects me to the main page. And in general, I think in the right direction? Maybe what I need is done somehow differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alekamisleforma, 2020-06-15
@alekamisleforma

In general, if someone needs to do something similar on WP, you can implement it as follows:
1) In the admin panel, create a page (for example, profile) and assign a pre-created template for it (for example, profile.php)
2) In the template page, we get the id passed by the link using This is the case when you want any user to be able to enter the user profile, and not just its owner. If you are creating a personal account, then you can get id like this
$userID = $_GET["id"];

$current_user = wp_get_current_user();
$userID = $current_user->ID;

Further on this id display everything that you need.
Keep in mind that if you want to give the user the opportunity to change personal data in the profile (and you will), then do not forget about verification. For example, it can be implemented by comparing the id obtained with $_GET["id"]; and id obtained with wp_get_current_user();
3) Going to the user's profile is carried out by links like localhost/profile/?id=1
4) In general, I don't know how correct this implementation of the task is, but it works as it should. I just couldn't implement normal links, ie. by type localhost/profile/username.I know that this is done in the .htaccess file using the mod_rewrite module, but its syntax is still too complicated for me, I will need to take some time and "meditate" on this thing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question