ElearningWorld.org

For the online learning world

Elearning WorldLMSMoodleTechnical

Moodle 4.x LMS: Show/Hide Profile Sections and Fields

Almost a year ago, we discussed how to Show/Hide user profile sections and fields in Moodle 3.9, 3.10 and 3.11. This month’s article is an update for those running Moodle 4.0 and Moodle 4.1 LTS. If you want to have better control of the sections and fields that appear when users create a new account and edit their profile pages in Moodle, you’ve come to the right place. Displaying just the fields you want will improve the user experience by reducing confusion and a sense of being overwhelmed. Users might also be more inclined to keep their profile information up to date.

In this article, we will look at techniques to:

  • Identify the IDs of a section and individual fields;
  • Automatically expand (open) sections that are collapsed by default; and
  • Hide unwanted sections and unnecessary fields.

The techniques described in this article have been tested on Moodle 4.0 and Moodle 4.1 LTS with the Boost, Classic, Trema, Moove, Adaptable, Fordson, eGuru, Klass, EnlightLite and may work with many other themes. However, you may need to adapt it for other versions of Moodle and other themes, especially premium ones. Some themes already include settings to control the visibility of some sections and profile fields. If your theme provides such settings, use those instead.

Hiding a category/section of profile fields

Important: Never hide a section that contains required fields. If you do, users may not be able to submit (save) the form if the fields are empty. Also note that this technique does not affect the visibility of fields in the mobile app, only when accessed from a web browser.

It is possible to completely hide one or more unwanted sections within a category/section on the Edit Profile pages by adding a little CSS to your theme through its settings. Unlike previous versions of Moodle, only custom section headings appear on the site registration New Account page so we need not be concerned with hiding these.

The first thing we need to do is to identify the ID associated with the sections you want to hide. Navigate to the Edit Profile page, right-click on the label of the section and select Inspect. In this case we want to locate the ID associated with the section called Additional name.

If you right-click on Additional names and select Inspect, you will see something that looks like this:

The section you are looking for will start with <fieldset…. The section id will start with id=”id_moodle_…”. In our example, it would be id_moodle_additional_names. This is the ID that you will need to note.

Section name IDs typically include:

  • User Picture: id_moodle_picture
  • Additional Names: id_moodle_additional_names
  • Interests: id_moodle_interests
  • Optional: id_moodle_optional

If you created additional sections of fields, they will be given a generic name id, regardless of what the field is called, such as id_category_1, id_category_2, etc.

With the section name ID in hand, you will now be able to completely hide the section by adding the following line to your theme’s SCSS field in its settings.

#page-user-editadvanced #id_additional_names {display: none;}

Expanding a collapsed section of profile fields by default

If you added custom user profile fields in a custom profile category, it can be difficult to get users to keep their information up to date since the field ends up in a collapsed section of the page. Here is how you can make Moodle expand collapsed sections by default using a little JavaScript. Note that in Moodle 4.x, standard section headings are not displayed on the site New Account page and additional sections are already expanded by default. Therefore, we only need to expand the section on the Edit Profile page.

Start by identifying the Section link ID. This will be different than the section name ID described in the previous section.

Navigate to the Edit Profile page, right-click on the label of the section and select Inspect. For example, in this case we might want to identify the link ID associated with the section called Additional name.

Once again, right-click on the section name and select Inspect.

Look a few lines down for the one that starts with <a data-toggle=. Follow that line until you get to the part that starts with id=. It will look something like id=”collapsedElement-X”. In this case, collapsedElement-2 is the ID you will need to note.

Standard section link IDs typically include:

  • User Picture: collapsedElement-1. By default in Moodle 4.0 and 4.1, the Picture section is already expanded, not collapsed.
  • Additional Names: collapsedElement-2
  • Interests: collapsedElement-3
  • Optional: collapsedElement-4

If you created additional category sections, they will continue with the same naming scheme. Example: collapsedElement-5, collapsedElement-6, etc.

With the correct section link ID in hand, you will now be able to expand the collapsed section by adding a few lines to Moodle’s settings. Navigate to Site Administration > Appearance > Additional HTML and add the following lines of JavaScript into the Before Body is Closed field:

<script>
if (document.getElementById('page-user-editadvanced')) {
   document.getElementById('collapseElement-2').click(); // Expand Additional Names section.
}
</script>

Explanation: This JavaScript will expand the Additional Names section on the Edit Profile page.

  • The line which begins with if…  just checks to make sure you are on the edit profile page.
  • The line that contains the .click() simulates the user clicking on the section to expand the Additional Names section.

If there is more than one section that you would like to automatically expand, add additional document.getElementByID… lines. Example:

<script>
if (document.getElementById('page-user-editadvanced')) {
   document.getElementById('collapseElement-2').click(); // Expand Additional Names section.
   document.getElementById('collapseElement-3').click(); // Expand Interests section.
   document.getElementById('collapseElement-4').click(); // Expand Optional section.
}
</script>

This will expand the Additional Names, Interests and Optional sections.

Hiding specific Moodle profile fields

Let’s say you have a school where all users are located in the same timezone: America/Toronto. There would be no point in including a profile field to change this. Not only would it be redundant and clutter up the list of profile fields, but a student might change it resulting in incorrect time-related information being displayed such as deadlines, live sessions or appointments. Here is how you can hide one or more specific profile fields instead of a whole section.

Start by identifying the ID of the field you want to hide. The technique previously used to identify a section name IDs can be used to find the ID of an individual field. In this case, right-click on the field name Timezone or the field name itself and select Inspect.

Because we want to affect a field instead of a section, we want to find the ID that is prefixed with “fitem_”. Example: fitem_id_timezone, not just id_timezone.

In this case, the id we are looking for is fitem_id_timezone.

Here is a list of standard Moodle profile field IDs that you might want to hide:

  • Email display: fitem_id_maildisplay
  • MoodleNet Profile: fitem_id_moodlenetprofile
  • City: fitem_id_city
  • Country: fitem_id_country
  • Timezone: fitem_id_timezone
  • Description: fitem_id_description
  • First name – phonetic: fitem_id_firstnamephonetic
  • Last name – phonetic: fitem_id_lastnamephonetic
  • Middle name: fitem_id_middlename
  • Alternate name: fitem_id_alternatename
  • ID number: fitem_id_idnumber
  • Institution: fitem_id_institution
  • Department: fitem_id_department
  • Phone: fitem_id_phone
  • Mobile phone: fitem_id_phone2
  • Address: fitem_id_address

Moodle can also potentially include custom profile fields. These IDs will be prefixed by fitem_id_profile_field_.

Since the Interests section only contains one field, you will need to hide the section instead of the field.

With the desired field ID in hand, you will now be able to hide the field in the profile form.

To hide the Timezone field, you would add the following lines to your theme’s CSS:

/* Hide the Timezone profile field in the General section. */
/* Not available on New Account page. */
#page-user-editadvanced #fitem_id_timezone {display: none;}

This will hide the timezone field on the Edit Profile page. This field does not appear on the Sign-up page for New Account registrations in Moodle 4.x so we do not need any CSS for that page in this case.

Minimal Profile and New Account pages using CSS

Here is an example of a clean and minimal New Account and Profile page that hides most of the optional sections and fields using CSS. You can add this to your theme’s setting in a field often called Raw SCSS, SCSS or CSS.

/* These fields are available on both the user's Edit Profile page 
   and the New Account sign-up registration page.
 */

/* Hide the City/Town profile field in the General section. */
#page-login-signup #fitem_id_city,
#page-user-editadvanced #fitem_id_city,

/* Hide the Country profile field in the General section. */
#page-login-signup #fitem_id_country,
#page-user-editadvanced #fitem_id_country,

/* Hide the first custom section - if you created one. */
#page-login-signup #id_category_1,
#page-user-editadvanced #id_category_1,

/* These fields are only available on the user's Edit Profile page.
 */

/* Hide the Email display profile field in the General section. */
#page-user-editadvanced #fitem_id_maildisplay,

/* Hide the MoodleNet profile field in the General section. */
#page-user-editadvanced #fitem_id_moodlenetprofile,

/* Hide the Timezone profile field in the General section. */
#page-user-editadvanced #fitem_id_timezone,

/* Hide the Description profile field in the General section. */
#page-user-editadvanced #fitem_id_description_editor,

/* Hide the User Picture section. */
#page-user-editadvanced #id_moodle_picture,

/* Hide the Additional names section. */
#page-user-editadvanced #id_moodle_additional_names,

/* Hide the Interests section. */
#page-user-editadvanced #id_moodle_interests,

/* Hide Optional section. */
#page-user-editadvanced #id_moodle_optional,

.nodisplay { /* The .nodisplay class is just a placeholder. */
    display: none!important;
}

Minimal Profile and New Account pages using JavaScript

Using JavaScript instead of CSS has the advantage of actually removing the fields from the form instead of just hiding them on the New Account and Profile pages.

To install the code, navigate to Site Administration > Appearance > Additional HTML and add the following lines of JavaScript into the Before Body is Closed field:

<script>
// These optional fields appear on both the New Account sign-up page and the Edit Profile page.
if (document.getElementById('page-login-signup') || document.getElementById('page-user-editadvanced')) {
    // Hide the City/Town profile field in the General section.
    document.getElementById('fitem_id_city').remove();

    // Hide the Country profile field in the General section.
    document.getElementById('fitem_id_country').remove();

    // Hide the first custom section - if you created one.
    // Only include this if you actually have a custom section that you want to remove. Otherwise, remove or
    // comment out the following line as it will cause a JavaScript error if you do not have a custom section.
    document.getElementById('id_category_1').remove();
}

// These fields only appear on the Edit Profile page.
if (document.getElementById('page-user-editadvanced')) {
    // Hide the Email display profile field in the General section.
    document.getElementById('fitem_id_maildisplay').remove();

    // Hide the MoodleNet profile field in the General section.
    document.getElementById('fitem_id_moodlenetprofile').remove();

    // Hide the Timezone profile field in the General section.
    document.getElementById('fitem_id_timezone').remove();

    // Hide the Description profile field in the General section.
    document.getElementById('fitem_id_description_editor').remove();

    // Hide the User Picture section.
    document.getElementById('id_moodle_picture').remove();

    // Hide the Additional names section.
    document.getElementById('id_moodle_additional_names').remove();

    // Hide the Interests section.
    document.getElementById('id_moodle_interests').remove();

    // Hide Optional section.
    document.getElementById('id_moodle_optional').remove();
}
</script>

Final thoughts and tips

If you intend to hide all of the fields in a section, it is not necessary to hide each field in the section. Simply hide the whole section as this will also remove the fields. However, since you cannot hide whole sections on the sign-up page, you may still need to hide individual fields.

Be sure to always check both the New Account and the Edit Profile pages to ensure that they display or hide the intended information.

Custom profile fields come with settings to control the visibility of the field. Use these settings to hide fields instead of JavaScript or CSS. Remember that most of these techniques only hide the fields, they don’t remove them from the page.

You can practice applying these changes to the current version of Moodle before applying them to your own Moodle site by going to the official Moodle Sandbox Demo site at https://sandbox.moodledemo.net/

Hope you find this information helpful in cleaning up your Moodle profile pages, simplifying and making account creation and maintenance simpler and easier for your users. Post a comment if you have any questions.

See you next month!

Michael Milette

Michael Milette
Follow me

Michael Milette

Michael Milette is the owner and an independent consultant with TNG Consulting Inc. in Canada. He works with government, non-profit organizations, businesses and educational institutions on Moodle-related projects. Michael writes about implementing Moodle LMS, developing in Moodle, Moodle administration, using the FilterCodes plugin (his own project), creating multi-language Moodle implementations and courses, and WCAG 2.1 accessibility.

3 thoughts on “Moodle 4.x LMS: Show/Hide Profile Sections and Fields

  • CarlosH

    Michael,
    Thanks for this article it put me on the right track on how to customize the user profile. However, on the how to expand sections using JavaScript, your proposed solution doesn’t work for me on version 4.0.6 (not tested, but possible also in 4.1 and 4.2). The reason is because the last possible “Additional HTML” option is “Before BODY is closed”. At that point there are two problems; the “collapseElement-x” ids are not available yet (so cannot be referred) and also the collapse Elements action has not been attached yet (click action will do nothing). The first problem could be solved by using a selector like “document.querySelector(‘#id_moodle_optional a’)” instead but the second problem can not solved as there is not click event yet.

    Debugging on how this comes to exists later, I see that it is put into a jQuery Deferred Promise by the “EXPAND ALL” “collapsemenu” component when the require “core_form/collapsesections” is called. Unfortunately this promise is not accessible at global level or even at the moment of the “Before BODY” section, this has not been created yet. Oh well, that should had been an easy solution by just adding an extra “.then” callback to the promise.

    Back to the drawing board. I didn’t want to put an ugly setTimeout (I know that will work). I wanted to put my own promise or event that will tell me when the collapse Elements are available and ready. I looked into the last things “collapsesections” does. It collects all the collapse Elements ids and put them into the attribute “aria-controls”. So, I see that I could use a MutationObserver that when an attribute is modified, it will trigger a callback and everything is ready. So I ended with code like this:

    if (document.getElementById(‘page-login-signup’) || document.getElementById(‘page-user-editadvanced’)) {
    const observer = new MutationObserver(() => {
    document.querySelector(‘#collapseElement-4’).click();
    document.querySelector(‘#collapseElement-5).click();
    observer.disconnect();
    });
    observer.observe(document.querySelector(‘.collapsemenu’), {attributeFilter: [‘aria-controls’]});
    }

    I simplified the full MutationObserver formal callback as I do not need to iterate on the records and I do not need to check which attribute mutated as I filtered to observe only one.

    I hope this save hours of frustration to anybody else with the same problem. Maybe you could improve your article, as it is referred on the Moodle documentation.

    Reply
  • Anonymous

    Hi Michael.
    Thank you for these fruitful tips.
    I need to make visible the default profile field “Mobile Phone” (under the “Optional” section on the user profile page) on the signup page. I don’t want to create a custom profile field to collect the mobile phone numbers since there is already a default field for that. But, I couldn’t find a way to make it visible or at least a required field. If possible, I’ll be able to send a 2-step verification code on login. I don’t want the users to go to the profile page and fill in that field after signup. That’s also not feasible to collect that data because most of the users don’t update their profile if it’s not a required field. Thank you.

    Reply
  • Some amazing tips and examples in there Michael – I learned a few new things – thanks so much for sharing !

    Reply

Add a reply or comment...