002_TF_Understanding-Options-in-Creating-FrameworksOne of the main features of the framework is the easy creation of options in the admin. Options can be added to admin pages, meta boxes or the theme customer. The code to insert an option into these areas are all the same.

An option can be inserted by using the function `createOption( $settingsArray )`. For example:

$areaToPlaceTheOption->createOption( array(
'name' => 'Name', // The name label of the option
'id' => 'option_id', // The ID of the option, this is used to get the saved value
'type' => 'text', // The type of the option
'default' => 'default value',
'desc' => 'A description' // A description shown near the option
) );​

In the code above, the variable `$areaToPlaceTheOption` corresponds to an admin page, meta box or theme customizer section object created earlier.

The arguments `name`, `id`, `type`, `default`, and `desc` are common arguments to all the available options. Some options may have additional arguments available for them. For example, the select drop down option has an additional setting called `options` which can be used to add an array of choices for the drop down option.

[zilla_alert style=”white”]Refer to the documentation for the complete list of all the options and their settings.[/zilla_alert]

Getting saved values differ (only slightly) depending on where the option is placed. If the option is in an admin page or the theme customizer, you can get the value with this:

echo $titan->getOption( 'option_id' );​

Meta box options on the other hand, are tied to the post they belong to. While inside a loop, you can get the option value by specifying the post ID in the function call:

echo $titan->getOption( 'option_id', get_the_ID() );​

[zilla_alert]Make sure you only perform your `getOption` calls from inside WordPress hooks and filters, or inside your theme template files. If the function is called earlier – for example, within the first lines in your `functions.php` – the framework might not have initialized yet.[/zilla_alert]

2 thoughts

Leave a Reply