<p>The <a href="https://craftcms.com/classreference/services/UserSessionService#getUser-detail" rel="nofollow noreferrer"><code>UserSessionService::getUser</code> method</a> returns <a href="https://craftcms.com/classreference/models/UserModel" rel="nofollow noreferrer">a <code>UserModel</code></a>.</p>

<p>You can retrieve the custom fields from a <code>UserModel</code> as follows:</p>

<pre><code>$userModel-&gt;getContent()-&gt;getAttribute('fieldName');
</code></pre>

<p>Or, following on from your example:</p>

<pre><code>craft()-&gt;userSession-&gt;getUser()-&gt;getContent()-&gt;getAttribute('fieldName');
</code></pre>

CC BY-SA 3.0
2017-04-28T11:15:57.367Z
I tried this and it works but now I have a relation field and that's returns 'null'
Maarten Heideman
CC BY-SA 3.0
2017-03-21T14:03:24.907Z
2017-03-21T16:07:04.783Z
2017-03-21T16:07:04.783Z
Stephen Lewis
<p>How get the custom user fields in a plugin? I can get the normal fields like firstName like this <code>craft()-&gt;userSession-&gt;user-&gt;firstName</code> but how i can get a custom field i tried <code>craft()-&gt;userSession-&gt;user-&gt;customfieldname</code> but that doesn't work.</p>

<plugin-development>
How get custom user fields in a plugin?
# How get custom user fields in a plugin?

(Asked by: username_0 on 2017-03-21)

How get the custom user fields in a plugin? I can get the normal fields like firstName like this `craft()->userSession->user->firstName` but how i can get a custom field i tried `craft()->userSession->user->customfieldname` but that doesn't work.

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2017-03-21. Score: 4 | Accepted answer

The UserSessionService::getUser method returns a UserModel.
You can retrieve the custom fields from a UserModel as follows:
$userModel->getContent()->getAttribute('fieldName');

Or, following on from your example:
craft()->userSession->getUser()->getContent()->getAttribute('fieldName');

> **Comments:**
> **username_0**: I tried this and it works but now I have a relation field and that's returns 'null'  

---

