Access User Custom Fields

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
DrDave
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Thu Mar 24, 2011 3:01 pm

Access User Custom Fields

Post by DrDave » Fri Jan 14, 2022 6:16 am

I'm using Joomla 3.10.4

I have two user custom fields, called season-joined and membership-status. I'm look for the php code that would allow me to do this:

If season-joined = "2021-2022" and membership-status = "Active" return true, otherwise, return false.

Any help is appreciated.

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 28414
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Access User Custom Fields

Post by Per Yngve Berg » Fri Jan 14, 2022 8:34 am

Mod. Note: Relocated topic from J4 to J3 forum.

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 13976
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Access User Custom Fields

Post by toivo » Mon Jan 17, 2022 5:07 am

Here is one way how to access user custom fields in an override of the profile view of the Users component in the Protostar template. Create the override in Template Manager.

Code: Select all

// insert after line 34 of templates/protostar/html/com_users/profile/default_custom.php
$seasonJoined		= '';
$membershipStatus	= '';
foreach ($tmp as $customField)
{
	switch ($customField->name)
	{
		case 'season-joined':
			$seasonJoined		= $customField->value;
			break;
		case 'membership-status':
			$membershipStatus	= $customField->value;
			break;
		default:
			echo '<br>Unknown custom field: ' . $customField->name;
	}
}
echo '<br>Season joined ' . $seasonJoined . ' Membership status ' . $membershipStatus;

The same code works also in a profile override in Joomla 4.x:

Code: Select all

// insert after line 29 of templates/cassiopeia/html/com_users/profile/default_custom.php
$seasonJoined		= '';
$membershipStatus	= '';
foreach ($tmp as $customField)
{
	switch ($customField->name)
	{
		case 'season-joined':
			$seasonJoined		= $customField->value;
			break;
		case 'membership-status':
			$membershipStatus	= $customField->value;
			break;
		default:
			echo '<br>Unknown custom field: ' . $customField->name;
	}
}
echo '<br>Season joined ' . $seasonJoined . ' Membership status ' . $membershipStatus;
Toivo Talikka, Global Moderator


Post Reply

Return to “Joomla! 3.x Coding”