Associate user data according to "/profile/username" url

Asked

Viewed 160 times

1

Hello I’m a beginner in php and mysql, I’ve searched everything that is singing and I still can’t understand how to associate my user url to id. I would like to know how to associate and display user data according to your url and not logged in user, I need this data to be public.

I use wordpress and the basis of the url is: /Author/user/
I used a wp_rewrite Function to leave it like this: /profile/user/

The code works well but displays data of all usernames...

<?php

$id = $_GET['id'];

$pdo = new PDO('mysql:host=localhost;dbname=', '', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$consulta = $pdo->query("SELECT user_nicename FROM users WHERE id= '$id' ");
while ($exibir = $consulta->fetch(PDO::FETCH_ASSOC)) {
    
    // minha consulta
	echo "fullname: {$exibir['user_nicename']} <br/>";
}
?>

  • Friend, a question. All users are authors, right? Even if they do not execute author function, because I saw that you changed the url with rewrite. But when added, do they enter the system as the correct author? /Author/user

1 answer

0


Hello! Being your users authors, then you can pick up the nickname by URL. This code needs to stay in the PHP file, Author.php. You find the default template on: Codex.wordpress.org/Author_templates

Use get_the_author_meta() to retrieve the author’s requested data.

$usuario = get_the_author_meta('user_nicename');

Retrieve user information for a given field, so you have the user ID. With the author nickname recovered from the URL, then use get_user_by().

Thus, you recover the user ID. Not from the logged in user, but from the author(nickname) that is in the URL

$user = get_user_by('slug',$usuario);
print_r($user->ID);

So retrieve the email:

echo $user->user_email;

Other option:

<?php
        global $wp_query;
        $curauth = $wp_query->get_queried_object();

        echo "Nome: " . $curauth->first_name . "<br />" .
             "Sobrenome: " . $curauth->last_name;   
/*   
    $curauth->aim;
    $curauth->description;
    $curauth->display_name;
    $curauth->first_name;
    $curauth->ID;
    $curauth->jabber;
    $curauth->last_name;
    $curauth->nickname;
    $curauth->user_email;
    $curauth->user_login;
    $curauth->user_nicename;
    $curauth->user_registered;
    $curauth->user_url;
    $curauth->yim;
*/
  • Unfortunately it is not showing the data I think I set up the wrong code.. Even so thank you you more clarified my understanding!

  • You need to go to the user’s page, follow url: /Author/user/

  • All this code is already in Author.php....

  • @Ewerton I edited the answer. Try the second option. Also compare your template with the default template at: https://codex.wordpress.org/Author_Templates Try: global $wp_query;&#xA; $curauth = $wp_query->get_queried_object();&#xA; &#xA; echo $curauth->ID;

  • Yeah, I did, and it didn’t work. I’m sorry the ignorance but in the query with the database: <code>SELECT FROM WHERE id = '$' </code> the ID to be called would not be <code>$user</code> ?

  • @Ewerton Do you want the username? If yes, just do it: global $wp_query;&#xA;$curauth = $wp_query->get_queried_object();&#xA; &#xA;echo "fullname:" . $curauth->first_name;

  • Strange I tried but did not display all 2 methods and the latter without the database.. Look I want this information to be public without having to login, I had already used curauth to display but the data was repeated even if I was in another Author, different url. Obs. I used Po because hearing that is safer... This is my site: link

  • @Ewerton You don’t need that call code of yours new PDO. It is not necessary to do this to get data in wordpress, it provides a layer to be used to return data. Take a test. Take the default template for the Author.php file, it’s the last code, where it’s written Here is a complete sample author.php file, which you can use as an example: on: https://codex.wordpress.org/Author_Templates Then use the 2nd option there in my reply(Other option:) Use the code that is there.

  • Opa managed to resolve I will edit/add to the post the solution. Thanks for the tips @Fabiano Monteiro !

  • @Good. But how did you solve it? In case it went in a very different way from my explanation, then you have q add an answer and not edit your question. Your question remains the same. Do not edit it and do not add the solution to the question. Solution of the provlema is add as answer. Now it’s gone with my tip. You’d have to mark it as the correct answer to your question. Got it?

  • 1

    Workaround: Apparently there was a conflict with the profile page that needed to be created because of the Userswp plugin. Even changing the template of the page "profile" to Author.php did not receive the data, but when disabling the plugin worked perfectly. I will now look for a way to use the plugin but with this option disabled->User Profile Page.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.