Skip to content

A helper class for registering WordPress settings pages with a simpler API.

License

Notifications You must be signed in to change notification settings

eighteen73/settings-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WordPress Settings API Class

Allows for easier creation of plugin settings pages by using an object orianted approach, with an API similar to the customizer.

An example

use Eighteen73\SettingsApi\SettingsApi;

$settings = new SettingsApi(
	'Plugin Settings',
	'Plugin Settings',
	'manage_options',
	'plugin-name',
	100,
	false, // set it to 'true' to create a top level menu
	''     // icon url to be used on top level menu
);

// Submenus: if top level menu is created.
$settings->set_submenu(
	'Submenu 1',
	'Submenu 1',
	'plugin-name-submenu',
	[ $this, 'callback_function' ]
);

// Section: Basic Settings.
$settings->add_section(
	[
		'id'    => 'plugin_name_basic',
		'title' => __( 'Basic Settings', 'plugin-name' ),
	]
);

// Section: Other Settings.
$settings->add_section(
	[
		'id'    => 'plugin_name_other',
		'title' => __( 'Other Settings', 'plugin-name' ),
	]
);

// Field: Text.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'      => 'text',
		'type'    => 'text',
		'name'    => __( 'Text Input', 'plugin-name' ),
		'desc'    => __( 'Text input description', 'plugin-name' ),
		'default' => 'Default Text',
	]
);

// Field: Number.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'                => 'text_no',
		'type'              => 'number',
		'name'              => __( 'Number Input', 'plugin-name' ),
		'desc'              => __( 'Number field with validation callback `intval`', 'plugin-name' ),
		'default'           => 1,
		'sanitize_callback' => 'intval',
	]
);

// Field: Email.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'   => 'email',
		'type' => 'email',
		'name' => __( 'Email', 'plugin-name' ),
		'desc' => __( 'Email field description', 'plugin-name' ),
	]
);

// Field: Password.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'   => 'password',
		'type' => 'password',
		'name' => __( 'Password Input', 'plugin-name' ),
		'desc' => __( 'Password field description', 'plugin-name' ),
	]
);

// Field: Textarea.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'   => 'textarea',
		'type' => 'textarea',
		'name' => __( 'Textarea Input', 'plugin-name' ),
		'desc' => __( 'Textarea description', 'plugin-name' ),
	]
);

// Field: Separator.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'   => 'separator',
		'type' => 'separator',
	]
);

// Field: Title.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'   => 'title',
		'type' => 'title',
		'name' => '<h1>Title</h1>',
	]
);

// Field: Checkbox.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'   => 'checkbox',
		'type' => 'checkbox',
		'name' => __( 'Checkbox', 'plugin-name' ),
		'desc' => __( 'Checkbox Label', 'plugin-name' ),
	]
);

// Field: Radio.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'      => 'radio',
		'type'    => 'radio',
		'name'    => __( 'Radio', 'plugin-name' ),
		'desc'    => __( 'Radio Button', 'plugin-name' ),
		'options' => [
			'yes' => 'Yes',
			'no'  => 'No',
		],
	]
);

// Field: Multicheck.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'      => 'multicheck',
		'type'    => 'multicheck',
		'name'    => __( 'Multile checkbox', 'plugin-name' ),
		'desc'    => __( 'Multile checkbox description', 'plugin-name' ),
		'options' => [
			'yes' => 'Yes',
			'no'  => 'No',
		],
	]
);

// Field: Multicheck with descriptions.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'      => 'multicheck_descriptions',
		'type'    => 'multicheck',
		'name'    => __( 'Multile checkbox with descriptions', 'plugin-name' ),
		'desc'    => __( 'Multile checkbox description', 'plugin-name' ),
		'options' => [
			'yes' => [
				'label' => 'Yes',
				'desc'  => 'Description of option',
			],
			'no' => [
				'label' => 'No',
				'desc'  => 'Description of option',
			],
		],
	]
);

// Field: Select.
$settings->add_field(
	'plugin_name_basic',
	[
		'id'      => 'select',
		'type'    => 'select',
		'name'    => __( 'A Dropdown', 'plugin-name' ),
		'desc'    => __( 'A Dropdown description', 'plugin-name' ),
		'options' => [
			'yes' => 'Yes',
			'no'  => 'No',
		],
	]
);

// Field: Image.
$settings->add_field(
	'plugin_name_other',
	[
		'id'      => 'image',
		'type'    => 'image',
		'name'    => __( 'Image', 'plugin-name' ),
		'desc'    => __( 'Image description', 'plugin-name' ),
		'options' => [
			'button_label' => 'Choose Image',
		],
	]
);

// Field: File.
$settings->add_field(
	'plugin_name_other',
	[
		'id'      => 'file',
		'type'    => 'file',
		'name'    => __( 'File', 'plugin-name' ),
		'desc'    => __( 'File description', 'plugin-name' ),
		'options' => [
			'button_label' => 'Choose file',
		],
	]
);

// Field: Color.
$settings->add_field(
	'plugin_name_other',
	[
		'id'          => 'color',
		'type'        => 'color',
		'name'        => __( 'Color', 'plugin-name' ),
		'desc'        => __( 'Color description', 'plugin-name' ),
		'placeholder' => __( '#5F4B8B', 'plugin-name' ),
	]
);

// Field: WYSIWYG.
$settings->add_field(
	'plugin_name_other',
	[
		'id'   => 'wysiwyg',
		'type' => 'wysiwyg',
		'name' => __( 'WP_Editor', 'plugin-name' ),
		'desc' => __( 'WP_Editor description', 'plugin-name' ),
	]
);

Credits

Heavily based on WP-OOP-Settings-API by Ahmad Awais.

About

A helper class for registering WordPress settings pages with a simpler API.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages