Skip to content

Settings

Carlos Moreira edited this page Jun 2, 2020 · 1 revision

The settings parameter allows you to create settings pages that will display under your Custom Post Type menu.

It should contain an array of settings models. Simplified example:

'settings'     => [
	'framework-demo-settings' => [
		'id'         => 'framework-demo-settings',
		'title'      => __( 'Plugin Settings', 'framework-demo' ),
		'capability' => 'manage_options',
		'menu_title' => __( 'Settings', 'framework-demo' ),
		'sections'   => [],
	],
],

The above code will add one settings page. You can then access your settings in the following way:

$settings = get_option('framework-demo-settings', false );

Saltus uses the Codestar Framework to generate the settings pages, so you can refer to their documentation for the available parameters for each settings page entry and each section. Some fields will vary, as you can see in our examples, but most of them will be the same.

Fields

When declaring the fields, there's some particularities that should be mentioned. As already mentioned, we're using the Codestart Framework to generate the settings pages, so all field types available in that framework can be used in a Saltus metabox model. However, when listing the fields in our model, we should have a reference id for each. Notice the example below:

	'fields' => [
		'paperback'  => [
			'title' => __( 'Paperback', 'framework-demo' ),
			'type'  => 'text',
			'desc'  => __( 'Number of pages', 'framework-demo' ),
		],
		'isbn'       => [
			'title' => __( 'ISBN', 'framework-demo' ),
			'type'  => 'text',
		],
		'desc'       => [
			'title' => __( 'Description', 'framework-demo' ),
			'type'  => 'textarea',
			'desc'  => __( 'Short description for this book', 'framework-demo' ),
		],
	],

You don't need to redeclare the id parameter for each field (but you can), as the reference id will be used.

Complete Example:

'settings'     => [
	'framework-demo-settings' => [
		'id'         => 'framework-demo-settings',
		'title'      => __( 'Plugin Settings', 'framework-demo' ),
		'capability' => 'manage_options',
		'menu_title' => __( 'Settings', 'framework-demo' ),
		'sections'   => [
			'general' => [
				'title'  => __( 'General', 'framework-demo' ),
				'desc'   => __( 'Basic settings that affect the plugin behaviour', 'framework-demo' ),
				'icon'   => 'fa fa-cog fa-lg',
				'fields' => [
					'gen01' => [
						'type'    => 'text',
						'title'   => __( 'Option 01', 'framework-demo' ),
						'desc'    => __( 'Description', 'framework-demo' ),
						'default' => '',
					],

					'gen02' => [
						'title'    => __( 'Option 02', 'framework-demo' ),
						'desc'     => __( 'description', 'framework-demo' ),
						'subtitle' => __( 'subtitle', 'framework-demo' ),
						'type'     => 'textarea',
					],
					'gen04' => [
						'title' => __( 'Option 03', 'framework-demo' ),
						'desc'  => __( 'Description', 'framework-demo' ),
						'type'  => 'switcher',
					],
					'gen05' => [
						'title' => __( 'Checkbox', 'framework-demo' ),
						'desc'  => __( 'Description', 'framework-demo' ),
						'type'  => 'checkbox',
					],
				],
			],


			'custom'  => [
				'title'  => __( 'CSS', 'framework-demo' ),
				'desc'   => __( 'Add custom styles to the pages loading the layouts.', 'framework-demo' ),
				'icon'   => 'fa fa-code fa-lg',
				'fields' => [
					'framework-demo_custom_css' => [
						'title' => __( 'Custom CSS', 'framework-demo' ),
						'desc'  => __( 'Custom CSS to load with the layouts', 'framework-demo' ),
						'type'  => 'code_editor',
					],
				],
			],

		],
	],
],