<p>It's not 100% clear from your question, but I'm guessing that by "update the settings" you mean actually storing the new settings in the database, which the <code>setSettings()</code> method will not do; it will only update your plugin's settings model. This means that any following calls to e.g. <code>getSettingsHtml()</code> should return the updated data, but it will not persist. </p>

<p>To store the new settings in the database, you'll need to use Craft's <a href="http://buildwithcraft.com/classreference/services/PluginsService#savePluginSettings-detail">PluginService</a>:</p>

<pre><code>craft()-&gt;plugins-&gt;savePluginSettings( $this, array( 'spriteDefs' =&gt; $this-&gt;getSpriteDefs( $url ) ) );
</code></pre>

<p>The <code>$this</code> variable in the call above refers to an instance of <a href="http://buildwithcraft.com/classreference/etc/plugins/BasePlugin">BasePlugin</a> – meaning that the above code will only work if called from your plugins main class. I notice that your code is contained within your plugin's <code>init()</code> method, so you should be fine, however if you ever want to update a plugin's settings from outside the main class (e.g. through a service), you would first need to fetch an instance of the plugin:</p>

<pre><code>$myPlugin = craft()-&gt;plugins-&gt;getPlugin( 'myPluginHandle' );
craft()-&gt;plugins-&gt;savePluginSettings( $myPlugin, $settings );
</code></pre>

CC BY-SA 3.0
2015-03-16T06:57:29.203Z
Yes thats correct, by update I meant persist - that is the correct term, sorry for the ambiguity - and I'm calling it within the plugin's main class yes. The savePluginsSettings did the trick :)
Daniel Paras
CC BY-SA 3.0
2015-03-16T11:59:02.913Z
No worries – temporarily manipulating the settings model within the app session lifetime cycle would be a valid use case as well, where ```setSettings``` would be the answer. Happy to hear my answer helped you out!
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-03-15T20:05:50.840Z
2015-03-15T20:11:20.940Z
2015-03-15T20:11:20.940Z
Mats Mikkel Rummelhoff
<p>Is there a way to programatically update a plugin's settings? I'm experimenting with <a href="http://buildwithcraft.com/classreference/etc/plugins/BasePlugin#setSettings-detail" rel="noreferrer">setSettings()</a> but not really getting anywhere - I'm still quite new to plugin development so there's a good chance i'm completely off target hehe.</p>

<p>For some context: I'm trying to update the plugin settings when an event is fired and picked up by a listener as follows:</p>

<pre><code>public function init()
{

    // add event listeners
    craft()-&gt;on('assets.onReplaceFile', function(Event $event) {

        //Load Asset
        $asset = $event-&gt;params['asset'];

        //Check if asset is the svg sprite
        if($asset-&gt;getUrl() == $this-&gt;getSettings()-&gt;spritePath){
            $url = craft()-&gt;getSiteUrl().$this-&gt;getSettings()-&gt;spritePath;
            $this-&gt;setSettings(array('spriteDefs'=&gt;$this-&gt;getSpriteDefs($url)));
        }

    });
}
</code></pre>

<p>getSpriteDefs returns an array.</p>

<p>Thanks in advance!</p>

CC BY-SA 3.0
2015-03-15T04:17:49.440Z
Have you tried a couple of `die()` statements to see if it's making it into your `if` statements?
Brad Bell
CC BY-SA 3.0
2015-03-16T07:01:31.050Z
Yes it was updating the settings model but not saving it to the db as mentioned in the selected answer below. My question might not have been 100% clear, apologies for that. thanks
Daniel Paras
<plugin-development>
How to programmatically update a plugin's settings?
# How to programmatically update a plugin's settings?

(Asked by: username_0 on 2015-03-14)

Is there a way to programatically update a plugin's settings? I'm experimenting with setSettings() but not really getting anywhere - I'm still quite new to plugin development so there's a good chance i'm completely off target hehe.
For some context: I'm trying to update the plugin settings when an event is fired and picked up by a listener as follows:
public function init()
{

    // add event listeners
    craft()->on('assets.onReplaceFile', function(Event $event) {

        //Load Asset
        $asset = $event->params['asset'];

        //Check if asset is the svg sprite
        if($asset->getUrl() == $this->getSettings()->spritePath){
            $url = craft()->getSiteUrl().$this->getSettings()->spritePath;
            $this->setSettings(array('spriteDefs'=>$this->getSpriteDefs($url)));
        }

    });
}

getSpriteDefs returns an array.
Thanks in advance!

**Tags:** plugin-development
> **Comments:**
> **username_1**: Have you tried a couple of `die()` statements to see if it's making it into your `if` statements?  
> **username_0**: Yes it was updating the settings model but not saving it to the db as mentioned in the selected answer below. My question might not have been 100% clear, apologies for that. thanks  

---

## 1 answers

### Answer by username_2 on 2015-03-15. Score: 10 | Accepted answer

It's not 100% clear from your question, but I'm guessing that by "update the settings" you mean actually storing the new settings in the database, which the setSettings() method will not do; it will only update your plugin's settings model. This means that any following calls to e.g. getSettingsHtml() should return the updated data, but it will not persist. 
To store the new settings in the database, you'll need to use Craft's PluginService:
craft()->plugins->savePluginSettings( $this, array( 'spriteDefs' => $this->getSpriteDefs( $url ) ) );

The $this variable in the call above refers to an instance of BasePlugin – meaning that the above code will only work if called from your plugins main class. I notice that your code is contained within your plugin's init() method, so you should be fine, however if you ever want to update a plugin's settings from outside the main class (e.g. through a service), you would first need to fetch an instance of the plugin:
$myPlugin = craft()->plugins->getPlugin( 'myPluginHandle' );
craft()->plugins->savePluginSettings( $myPlugin, $settings );

> **Comments:**
> **username_0**: Yes thats correct, by update I meant persist - that is the correct term, sorry for the ambiguity - and I'm calling it within the plugin's main class yes. The savePluginsSettings did the trick :)  
> **username_2**: No worries – temporarily manipulating the settings model within the app session lifetime cycle would be a valid use case as well, where ```setSettings``` would be the answer. Happy to hear my answer helped you out!  

---

