I am learning craft and trying add settings page for my plugin. I read that need to define settings like,

class CocktailRecipesPlugin extends BasePlugin
    // ...

    protected function defineSettings()
        return array(
            'cocktailCategories' => array(AttributeType::Mixed, 'default' => array('Sours', 'Fizzes', 'Juleps')),

So isn't clear and there is no explanation what the array return and how to use it inside the template.

Thank you

1 Answer 1


How are you trying to use it in the template?

Below is a very basic way of how to display a setting in a front end twig template.

First define your settings with the defineSettings() method (as you already have).

protected function defineSettings()
    return array(
        'mySetting' => array(AttributeType::String, 'required' => true)

Then create a directory in the root of your plugin folder and call it “templates”. Create a new .html or .twig file in that folder and call it “_settings.html”. Inside of that you could put the following:

{% import "_includes/forms" as forms %}
{{ forms.textField({
        label: "My Setting"|t,
        id: 'mySetting',
        name: 'mySetting',
        instructions: "Input your setting"|t,
        value: settings.mySetting,
        errors: settings.getErrors('mySetting')

After this go back to your primary plugin class and add the getSettingsHtml() method:

public function getSettingsHtml()
    return craft()->templates->render(‘pluginHandle/_settings', array(
        'settings' => $this->getSettings()

Now, if you go into admin/settings/plugins you can click on your plugin and there will be an input field there where you can save the setting. If you wanted to get this value you could add a method to your service like this:

public function getMySetting()
    $plugin = craft()->plugins->getPlugin(‘pluginHandle');
    $settings = $plugin->getSettings();

    return $settings->mySetting;

You could call this anywhere throughout your plugin like this:


To do that on the front end in a twig template, just create a method in the variables class. It would look something like this:

public function getMySetting()
    return craft()->pluginHandle_serviceName->getMySetting();

In your front end twig template you could call it like this;

{{ craft.pluginHandle.getMySetting() }}

I hope any of this clears it up a bit.

  • 12
    This is really useful it would be great if the plugin settings doc page had more on it like this
    – mattl
    Commented Feb 6, 2015 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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