<p>See <a href="https://craftcms.stackexchange.com/a/764/57">here</a> for how to add a field to a user's field layout.</p>

<p>Users, Globals, Tags, Categories, etc. are all <a href="https://craftcms.com/docs/plugins/working-with-elements" rel="nofollow noreferrer">elements</a> in Craft so they behave very similarly.</p>

<p>You can also check out Craft's own <a href="http://buildwithcraft.com/classreference/services/InstallService" rel="nofollow noreferrer">InstallService</a>'s private <code>_createDefaultContent()</code> method for examples on how Craft creates the default content on a fresh install of Craft.</p>

CC BY-SA 3.0
2017-04-09T23:19:17.387Z
2017-04-09T23:19:17.387Z
2017-04-13T12:50:59.930Z
Brad Bell
<p>is there a way to add a field to a global set by code ? I figured out how to create a global:</p>

<pre><code>$global = new GlobalSetModel();
$global-&gt;name = 'GlobalName';
$global-&gt;handle = 'globalHandle';
craft()-&gt;globals-&gt;saveSet($global)
</code></pre>

<p>But I can't figure out how to add a field to this newly create global.</p>

<plugin-development>
Add a field to a global set
# Add a field to a global set

(Asked by: username_0 on 2017-04-09)

is there a way to add a field to a global set by code ? I figured out how to create a global:
$global = new GlobalSetModel();
$global->name = 'GlobalName';
$global->handle = 'globalHandle';
craft()->globals->saveSet($global)

But I can't figure out how to add a field to this newly create global.

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2017-04-09. Score: 1 | Accepted answer

See [here](https://craftcms.stackexchange.com/a/764/57) for how to add a field to a user's field layout.

Users, Globals, Tags, Categories, etc. are all [elements](https://craftcms.com/docs/plugins/working-with-elements) in Craft so they behave very similarly.

You can also check out Craft's own [InstallService](http://buildwithcraft.com/classreference/services/InstallService)'s private `_createDefaultContent()` method for examples on how Craft creates the default content on a fresh install of Craft.


---

