wp plugin -19

THE SETTINGS API

The Settings API enables you to handle the simple tasks:

Tell WordPress that you are going to use some new options and how you want them displayed.

Specify a function that will sanitize user inputs. And let WordPress transparently manage for you the cumbersome and repetitive parts:

Draw most of the option page itself.

Monitor form submission and handle $_POST data.

Create and update options if needed.

Wield all the required security measures and hidden

 

Settings API Functions

The Settings API functions consist of three steps:

1. First tell WordPress the new settings you want it to manage for you. Doing so adds your settings into a list of authorized options (also known as whitelisting).

2. Next defi ne the settings (text areas, input boxes, and any HTML form element) and how they will be visually grouped together in sections.

3. Tell WordPress to display your settings in an actual form.

 

Creating the Plugin Administration Page

< ?php

// Add the admin options page

add_action(‘admin_menu’, ‘boj_myplugin_add_page’);

function boj_myplugin_add_page() {

add_options_page( ‘My Plugin’, ‘My Plugin’, ‘manage_options’,

‘boj_myplugin’, ‘boj_myplugin_options_page’ );

}

// Draw the options page

function boj_myplugin_options_page() {

? >

< div class=”wrap” >

< ?php screen_icon(); ? >

< h2 > My plugin < /h2 >

< form action=”options.php” method=”post” >

< /form > < /div >

< ?php

}

? >

 

Registering New Settings

< ?php

register_setting(

‘boj_myplugin_options’,

‘boj_myplugin_options’,

‘boj_myplugin_validate_options’

);

? >

 

Defining Sections and Settings

< ?php

add_settings_section(

‘boj_myplugin_main’,

‘My Plugin Settings’,

‘boj_myplugin_section_text’,

‘boj_myplugin’

);

add_settings_field(

‘boj_myplugin_text_string’,

‘Enter text here’,

‘boj_myplugin_setting_input’,

‘boj_myplugin’,

‘boj_myplugin_main’

);

? >

 

n

posted on 2012-12-05 18:46  kalintw  阅读(135)  评论(0)    收藏  举报

导航