Site logo

Archived topic

Get username on link

3 replies · Started by Jesus Higuerey on August 5, 2018

Viewing posts 1–4 of 4

Hi,

This is not related to generatepress but I didn't know who else to ask.

So I've been making a small site for a local university back home. We will upload the certificates once a student has passed the courses. All the students enrolled are signed up, where the "Username" is the government ID number which is not private and can be accessed by anyone without having any problems.

I have a dedicated page for "downloading the course certificate" in which only logged in users will be able to access.

I found a way of making it work by changing the ID manually through the DB or using a plugin, which is no the best way. I achieved this by using the following code.

<?php if ( is_user_logged_in() ) : ?>
<a href="http://sub.domain.com<?php echo get_current_user_id(); ?>.pdf">LINK</a>
<?php else : ?>
<div class="attention">Necesitas Iniciar Sesion para ver tu perfil.</div>
<?php endif; ?>

This printed the User ID: sub.domain.com/USERID.pdf

That worked wonders but I needed to change the user ID manually everytime a student got enrolled. This would be okay if there were like 10-20 students but not really if you have +100.

So what I want to do is to print the username, which is the government ID, to the link

NOTE: I've been checking out the function reference to get the username but I've been unsuccessful.

This is what I want to achieve: sub.domain.com/USERNAME.pdf

This sounds so confusing but my English isn't the best, so I apologize for that.

Hi there,

Try this:

<?php if ( is_user_logged_in() ) : 
    $current_user = wp_get_current_user(); 
    ?>
    <a href="http://sub.domain.com<?php echo $current_user->user_login; ?>.pdf">LINK</a>
<?php else : ?>
    <div class="attention">Necesitas Iniciar Sesion para ver tu perfil.</div>
<?php endif; ?>

Let me know :)

Thank you so much, Tom. It works wonders.

You're welcome :)

This archived topic is closed to new replies.