Class Name:

MP_CORE_Settings

Purpose:

To simplify and speed up the process of creating settings pages in WordPress.

Overview:

This class sets up a settings page with tabs for multiple settings groups. It uses an associative array to create the page. The tabs are added separately along with the settings themselves. A knowledge of the WordPress Settings API would be useful when going through this. This class is not intended as a complete work-around for the settings class, but rather, as a way to reduce the amount of code when creating settings pages with tabs.

Usage:

new MP_CORE_Settings($args);

Example Settings Page:

Take note of the Settings Page Slug used here (in the ‘slug’ slot of the array). In this example it is called “my_settings_page“. This is used in a few different places – including in the “Adding Tabs” section below.

If you are creating a new settings page and want to change the Settings Page Slug, each occurrence of the Settings Page Slug must match. The easiest way would be to do a find-and-replace of the term “my_settings_page“.

function my_settings_page(){

	$args = array('title' => 'My Settings Page', 'slug' => 'my_settings_page', 'type' => 'dashboard');

	//Initialize settings class
        global $my_settings_page;
	$my_settings_page = new MP_CORE_Settings($args);

}
add_action('plugins_loaded', 'my_settings_page');

For the $args array above, there are many different options to create different placements of pages.

Click here for more info on setting the $args array.
http://mintplugins.com/doc/settings-class-args/

Adding Tabs:

For each tab of settings you wish to add to the settings page we created above, duplicate the following section. Take special note of the Tab Slug. In this case it is “general“. When you duplicate the following for each new tab, you will have to replace each occurrence of the word “general” to match the new Tab Slug.

So if your second Tab Slug is called “second” for example, you need to replace each occurrence of the word “general” with “second“.

IMPORTANT: The first tab on a settings page must always have a Tab Slug of “general“.

The Settings Page Slug (as seen above) is used as the first part of the filter hook when adding a new tab. The filter hook to add a new tab is:

Settings Page Slug + _new_tab_hook

In this example, because the Settings Page Slug is “my_settings_page”, the filter hook for a new tab becomes:

my_settings_page_new_tab_hook
* Create new tab
*/
function my_settings_page_general_new_tab( $active_tab ){

	//Create array containing the title and slug for this new tab
	$tab_info = array( 'title' => __('My Tab' , 'textdomain'), 'slug' => 'general' );

	global $my_settings_page; 
        $my_settings_page->new_tab( $active_tab, $tab_info );

}
//Hook into the new tab hook filter contained in the settings class in the Mint Plugins Core
add_action('my_settings_page_new_tab_hook', 'my_settings_page_general_new_tab');

/**
* Create settings
*/
function mp_sermon_settings_general_create(){

	register_setting(
		'my_settings_page_general',
		'my_settings_page_general',
		'mp_core_settings_validate'
	);

	add_settings_section(
		'general_settings',
		__( 'General Settings', 'textdomain' ),
		'__return_false',
		'my_settings_page_general'
	);

        add_settings_field(
		'my_field_slug',
		__( 'My Field Title', 'textdomain' ), 
		'mp_core_textbox',
		'my_settings_page_general',
		'general_settings',
		array(
			'name'        => 'my_field_slug',
			'value'       => mp_core_get_option( 'my_settings_page_general',  'my_field_slug'),
			'description' => __( 'Description of Setting', 'textdomain' ),
			'registration'=> 'my_settings_page_general',
		)
	);

       add_settings_field(
                'my_field_slug_dropdown',
		__( 'My DropDown Field Title', 'textdomain' ), 
		'mp_core_select',
		'my_settings_page_general',
		'general_settings',
		array(
			'name'        => 'my_field_slug_dropdown',
			'value'       => mp_core_get_option( 'my_settings_page_general',  'my_field_slug_dropdown'),
			'description' => __( 'Description of Setting', 'textdomain' ),
			'registration'=> 'my_settings_page_general',
                        'options'=> array( 'option_value_1' => 'Option Value 1', 'option_value_2' => 'Option Value 2' )
		)
	);

 

} add_action( 'admin_init', 'my_settings_page_general_create' );

Creating Sections and Fields (as seen above):

A section is an area on the settings page which contains fields. This is a good way to organize fields into groups under a heading.

add_settings_section($id, $title, $callback, $page)
  • $id – String which settings use to  identify their section.
  • $title – Title of the section.
  • $callback – Set to ‘__return_false’
  • $page – Settings Page Slug + Tab Slug. In our example above that would be ‘my_settings_page‘ + ‘general‘ = ‘my_settings_page_general

To add a field use the following:

 add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array())
  • $id – String for use in the ‘id’ attribute of tags.
  • $title – Title of the field.
  • $callback – The Mint Plugins Core has many built-in callbacks like media upload and color pickers. Click Here to see all callbacks.
  • $page – This will be the same as the $page variable above.
  • $section – This will match the $id of the section above. This tells this field which section to exist within.
  • $args(array) – Additional arguments
    • ‘name’ – This matches the $id of this field. (See just above at the $id variable)
    • ‘value’ – This is the value that will be in the field. Recommended: mp_core_get_option( ‘my_settings_page_general’, $id)
    • ‘description’ – The description of this field.
    • ‘registration’- ‘my_settings_page_general’
    • ‘options’ (array) – This is only necessary if you are using a dropdown field by using the ‘mp_core_select’ callback function.

Using the settings in your project:

To get a setting from the database, simply use the mp_core_get_option function.

It takes 2 parameters. The first is the Settings Page Slug, and the second is the field id.

 $my_var = mp_core_get_option( 'my_settings_page_general',  'my_field_slug');