<p>First of all, you will need the Pro package to be able to customise the user profile.</p>

<p>Then you would have to use the <em>onAfterInstall</em> and <em>onBeforeUninstall</em> events in your plugin file. These events are called when installing and uninstalling the plugin.</p>

<p>After the install, you need to create a new field and add it to the user field layout. And before uninstall, you have to remove the field from the layout and delete the field itself.</p>

<p>The code below should work, but it doesn't keep the required fields (I didn't find a way to get them using the available functions).</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php

namespace Craft;

class UserfieldPlugin extends BasePlugin
{
    public function getName()
    {
        return Craft::t('Userfield');
    }

    public function getVersion()
    {
        return '0.9.1';
    }

    public function getDeveloper()
    {
        return 'Mario';
    }

    public function getDeveloperUrl()
    {
        return 'http://craftshake.com';
    }

    public function onAfterInstall()
    {
        // Create the third party field

        $thirdPartyField = new FieldModel();
        $thirdPartyField-&gt;groupId      = 1;
        $thirdPartyField-&gt;name         = Craft::t('Third Party');
        $thirdPartyField-&gt;handle       = 'thirdParty';
        $thirdPartyField-&gt;translatable = false;
        $thirdPartyField-&gt;type         = 'PlainText';

        craft()-&gt;fields-&gt;saveField($thirdPartyField);

        // Create the new user field layout

        $fieldLayout = craft()-&gt;fields-&gt;getLayoutByType(ElementType::User);
        $fieldsIds = $fieldLayout-&gt;getFieldIds();
        $fieldsIds[] = $thirdPartyField-&gt;id;

        craft()-&gt;fields-&gt;deleteLayoutsByType(ElementType::User);

        $fieldLayout = craft()-&gt;fields-&gt;assembleLayout(
            array(
                Craft::t('Profile') =&gt; $fieldsIds,
            ),
            array(),
            false
        );
        $fieldLayout-&gt;type = ElementType::User;

        craft()-&gt;fields-&gt;saveLayout($fieldLayout, false);
    }

    public function onBeforeUninstall()
    {
        // Get third party field
        $thirdPartyField = craft()-&gt;fields-&gt;getFieldByHandle('thirdParty');

        // Remove field from layout
        $fieldLayout = craft()-&gt;fields-&gt;getLayoutByType(ElementType::User);
        $fieldsIds = $fieldLayout-&gt;getFieldIds();
        $fieldsIds = array_diff($fieldsIds, array($thirdPartyField-&gt;id));

        craft()-&gt;fields-&gt;deleteLayoutsByType(ElementType::User);

        $fieldLayout = craft()-&gt;fields-&gt;assembleLayout(
            array(
                Craft::t('Profile') =&gt; $fieldsIds,
            ),
            array(),
            false
        );
        $fieldLayout-&gt;type = ElementType::User;

        craft()-&gt;fields-&gt;saveLayout($fieldLayout, false);

        // Delete field
        craft()-&gt;fields-&gt;deleteField($thirdPartyField);
    }

}
</code></pre>

<p>I hope this helps.</p>

<p>Mario</p>

CC BY-SA 3.0
2014-07-06T03:52:41.543Z
Mario - that's great, thank you! I'm assuming this would then display as a field within the layout for users that a CP admin could then delete if they wanted to? If possible I'd like to make it a hidden field, or at least one they couldn't remove and was hoping I could somehow extend the User model - but this approach would work still
Adam
CC BY-SA 3.0
2014-07-06T12:01:53.027Z
Indeed this adds a field to the user field layout and it could be remove by admin. To hide a field or extend User, you would have to ask @brandon-kelly or Brad Bell if this is possible.
Mario
CC BY-SA 3.0
2014-07-05T16:14:51.950Z
2014-07-05T16:14:51.950Z
Mario
<p>Within a plugin I'm working on, I'll be adding user synchronisation with a third-party database to enable SSO, so will be creating users within Craft based on an interaction from this other service.</p>

<p>I'd also like to associate the user ID from the other service against the Craft User model. Obviously you can create custom fields from within the CP - but is there a way for a plugin to automatically add a custom field so the admin doesn't need to do it themselves?</p>

<plugin-development><users>
Is it possible to add a custom field to User model from a plugin?
# Is it possible to add a custom field to User model from a plugin?

(Asked by: username_0 on 2014-07-05)

Within a plugin I'm working on, I'll be adding user synchronisation with a third-party database to enable SSO, so will be creating users within Craft based on an interaction from this other service.

I'd also like to associate the user ID from the other service against the Craft User model. Obviously you can create custom fields from within the CP - but is there a way for a plugin to automatically add a custom field so the admin doesn't need to do it themselves?

**Tags:** plugin-development, users


---

## 1 answers

### Answer by username_1 on 2014-07-05. Score: 6 | Accepted answer

First of all, you will need the Pro package to be able to customise the user profile.
Then you would have to use the onAfterInstall and onBeforeUninstall events in your plugin file. These events are called when installing and uninstalling the plugin.
After the install, you need to create a new field and add it to the user field layout. And before uninstall, you have to remove the field from the layout and delete the field itself.
The code below should work, but it doesn't keep the required fields (I didn't find a way to get them using the available functions).
groupId      = 1;
        $thirdPartyField->name         = Craft::t('Third Party');
        $thirdPartyField->handle       = 'thirdParty';
        $thirdPartyField->translatable = false;
        $thirdPartyField->type         = 'PlainText';

        craft()->fields->saveField($thirdPartyField);

        // Create the new user field layout

        $fieldLayout = craft()->fields->getLayoutByType(ElementType::User);
        $fieldsIds = $fieldLayout->getFieldIds();
        $fieldsIds[] = $thirdPartyField->id;

        craft()->fields->deleteLayoutsByType(ElementType::User);

        $fieldLayout = craft()->fields->assembleLayout(
            array(
                Craft::t('Profile') => $fieldsIds,
            ),
            array(),
            false
        );
        $fieldLayout->type = ElementType::User;

        craft()->fields->saveLayout($fieldLayout, false);
    }

    public function onBeforeUninstall()
    {
        // Get third party field
        $thirdPartyField = craft()->fields->getFieldByHandle('thirdParty');

        // Remove field from layout
        $fieldLayout = craft()->fields->getLayoutByType(ElementType::User);
        $fieldsIds = $fieldLayout->getFieldIds();
        $fieldsIds = array_diff($fieldsIds, array($thirdPartyField->id));

        craft()->fields->deleteLayoutsByType(ElementType::User);

        $fieldLayout = craft()->fields->assembleLayout(
            array(
                Craft::t('Profile') => $fieldsIds,
            ),
            array(),
            false
        );
        $fieldLayout->type = ElementType::User;

        craft()->fields->saveLayout($fieldLayout, false);

        // Delete field
        craft()->fields->deleteField($thirdPartyField);
    }

}

I hope this helps.
username_1

> **Comments:**
> **username_0**: username_1 - that's great, thank you! I'm assuming this would then display as a field within the layout for users that a CP admin could then delete if they wanted to? If possible I'd like to make it a hidden field, or at least one they couldn't remove and was hoping I could somehow extend the User model - but this approach would work still  
> **username_1**: Indeed this adds a field to the user field layout and it could be remove by admin. To hide a field or extend User, you would have to ask @brandon-kelly or Brad Bell if this is possible.  

---

