How to Get a Current User name in HTML block

How to Get a Current User name in HTML block

by Dnyaneshwar Somwanshi -
Number of replies: 3

I want to display Current user name in html block as well as on label . following code is not

 helping me. there is another same discussion but that is locked. any help is highly appreciated.

<?php
require('......../config.php');  // depends on your 'depth'. See other files in the same place
require_login(); // almost all pages require the user to be logged in
echo $USER->username;

Average of ratings: -
In reply to Dnyaneshwar Somwanshi

Re: How to Get a Current User name in HTML block

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Dnyaneshwar,

I think you would be better off to use the Generico Filter plugin to add names. It comes with a number of widgets/bundles of which username, firstname, and lastname are included. It then becomes just a matter of a couple of clicks to have the name shown wherever you want it. It can be placed just about anywhere you can open an Atto editor. You then also have the advantage of having many other widgets/bundles for other things.

Average of ratings: Useful (1)
In reply to Dnyaneshwar Somwanshi

Re: How to Get a Current User name in HTML block

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

As Jon and Al said, if you just want the name in a regular html block you do not need to code anything. With the help of the Generico or FilterCodes plugins, you can do quite a lot. I would not advise PHP coding unless you really need to. Most of the time it is not necessary and will hold you back later.

However if you do need/want to PHP code. There is a function that will prepare the users fullname. In that case try:

fullname($USER) or fullname($USER->id)


Average of ratings: Useful (1)