For instance I'm writing a plugin which is going to be periodically pulling stuff from Twitter, so I'd like to keep a hold of when the plugin was last run and maybe some Ids relevant to Twitter that sort of thing.

I know there is the settings array which you use for user defined values, should I also use it for other stuff that the user can't change, or is there a more sensible place to stick those?


You could create a Record which will give you a custom table to store your twitter related settings and update them as needed but I think that your use case is still simple enough that you could leave them in the settings array and then use the Plugins Service to update those settings.

This is how I would do it if I chose to use defineSettings()

// MyPlugin.php
public function defineSettings()
   return array(
        // ...
        'twitterApiKey' => array(AttributeType::String, 'default' => '5omeRand0mstriNg'),
        'twitterUsername' => array(AttributeType::String, 'default' => 'someone'),
// MyService.php
public function saveTwitterSettings(array $settings=array())
    $plugin = craft()->plugins->getPlugin('myplugin');
    $savedSettings = $plugin->getSettings()->getAttributes();
    $newSettingsRow = array_merge($savedSettings, $settings);

    return craft()->plugins->savePluginSettings($plugin, $newSettingsRow);

Feel free to follow up with questions/comments if you have any.


  • 1
    Thanks Selvin, Records was the piece of the puzzle I was missing. I do also want to keep a log of each run of the plugin, so I think in this instance a Record may make more sense. Your code will come in handy next time for instances where a Record is a little too much. – Steve Holland Jun 20 '14 at 13:19
  • Thanks for adding the link to the docs for the record @Steve and yes, in that case you'll definitely need one. Craft makes it a joy to interact with the database via records and the query builder: ) – Selvin Ortiz Jun 20 '14 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.