Settings page

Some explanation on settings (options) page, for a plugin in WordPress; here the plugin is localized, so every text is written with localization functions __() or _e(), the text domain is MMP_TEXT_DOMAIN.

  1. Create settings page: you need to have a function, here ‘mmp_admin_menu_action’ add_action('admin_menu', 'mmp_admin_menu_action'); called by hook admin_menu
  2. In this function, add the options page rendering function:$page = add_options_page(__('MultiMediaPublish Options', MMP_TEXT_DOMAIN), __('MultiMediaPublish', MMP_TEXT_DOMAIN), 'manage_options', __FILE__, 'mmp_render_options_page');
    this function defines:

    1. the title of the options page in the browser tag: ‘MultiMediaPublish Options’,
    2. the menu title in dashboard: here ‘MultiMediaPublish’
    3. the capability required to open this page: ‘manage_options’
    4. the slug name of the page, which will be used to make an url to the page: here we choose to use the file name, by using the magic constant of PHP ‘__FILE__’ which return the name of the file it is placed in.
    5. at last the function in charge to display the content of the options page, where you place the forms buttons aso to set the options of your plugin.

Now you have defined the function to display your options (settings) page, you need to:

  1. register the options you want to manage
  2. display this page in the defined function, that is define forms fields and buttons to enter the data and submit it
  3. validate data and update (record) options

To help you doing all these tasks with as much efficiency and security as possible, WP provides you special functions and display classes named settings API. These make possible to add more settings fields to existing setting pages (pagese xisting in WP, like “general”, “reading”, “writing”… or pages added by another plugin…) with functions like add_settings_section and add_settings_field.

As you already had created your own options page, you need to use settings API to manage options. If you have several types of options to manage, you may organize these options in several tabs that present grouped options together. First you have to hook in ‘admin’_init to register your options with register_setting function; register as many groups you have tabs to display:

add_action('admin_init', 'mmp_admin_init_action');

In mmp_admin_init_action function, loop on register_setting on an options array:
foreach($options as $option) {
register_setting($option['group'],$option['name'],$option['callback']);
}

  • $option['group'] is the name of the group of settings
  • $option['name'], the name of WP option that will be created (option you can read with WP standard function get_option
  • $option['callback'] function that will be called to validate data submitted by the settings form of a tag.

Display the options page with tabs (‘mmp_render_options_page‘ defined by add_options_page) , in each tab display form with matched options forms, and first get the active tab to display with somthing like that ($_GET[ 'tab' ] will be set by the tab link later, $tab['slug'] being tab slug (unique id)  :
/////// get active tab
$active_tab='general';
if( isset( $_GET[ 'tab' ] ) ) {
foreach($tabs as $tab) {
if($_GET[ 'tab' ]==$tab['slug']) $active_tab=$tab['slug'];
}
}

then use classes ‘nav-tab-wrapper’, ‘nav-tab’ and ‘nav-tab-active’ to render tabs on your page:

<h2>
<?php
if (!empty($tabs)) {
foreach ($tabs as $tab) {
$addclass= ($tab['slug']==$active_tab)? 'nav-tab-active' : '';
?>
<a href="?page=<?php echo plugin_basename(__FILE__); ?>&tab=<?php echo $tab['slug']; ?>"><?php echo $tab['caption']; ?> </a>
<?php
}
}
?>
</h2>

 

Where $tab['slug'] is the slug as seen before and $tab['caption'] the caption being displyed at th top of the tab.

Then switch select or loop on each tab slug to display the forms; you have to

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *