The next step following the database integration is a simple settings page to change the plugin options. This is achieved with another hook to add an item to the menu in the admin area:
[code lang=”php”]add_action(‘admin_menu’, array(&$this, ‘Admin_Menu’));[/code]
This alone doesn’t do anything other than telling WordPress to call the ‘Admin_Menu’ function at the right time, the function then tells WordPress the menu item to add and which page to open via the add_options_page API function:
[code lang=”php”] /**
* Create admin menu settings item
*
* @return void
*/
function Admin_Menu() {
add_options_page(‘Thumbshot Preview Settings’,
‘Thumbshot Preview’,
8,
basename(__FILE__),
array(&$this, ‘Admin_Settings’));
}[/code]
This creates the page link on the menu as ‘Thumbshot Preview’ and informs which file (the current plugin PHP file) and which function to call ‘Admin_Settings’. The number 8 refers to the user level required to access the page, in this case it is only available to the WordPress admin.
The Admin_Settings function simply outputs some HTML and also provides some very basic form validation and obviously also does the database updates. I have copied the styles and layouts from a standard WordPress settings page to ensure my page is consistent with the default styles. Here is the completed function:
[code lang=”php”] /**
* Display the admin setting page
*
* @return void
*/
function Admin_Settings() {
$msg = null;
// If form submitted save options
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {
// If free mode, very loose validation as most options are not needed
if ($_POST[‘tsp_mode’] == ‘free’) {
update_option(‘WP_Thumbshot_Preview_Mode’, ‘free’);
if (is_numeric($_POST[‘tsp_cid’])) {
update_option(‘WP_Thumbshot_Preview_Cid’, $_POST[‘tsp_cid’]);
}
if (is_numeric($_POST[‘tsp_width’])) {
update_option(‘WP_Thumbshot_Preview_Width’, $_POST[‘tsp_width’]);
}
if (is_numeric($_POST[‘tsp_height’])) {
update_option(‘WP_Thumbshot_Preview_Height’, $_POST[‘tsp_height’]);
}
$msg = ‘Settings saved.';
}
// For paid mode make sure the cid is numeric or do not save anything
if ($_POST[‘tsp_mode’] == ‘paid’) {
if (is_numeric($_POST[‘tsp_cid’])) {
update_option(‘WP_Thumbshot_Preview_Mode’, ‘paid’);
update_option(‘WP_Thumbshot_Preview_Cid’, $_POST[‘tsp_cid’]);
if (is_numeric($_POST[‘tsp_width’])) {
update_option(‘WP_Thumbshot_Preview_Width’, $_POST[‘tsp_width’]);
}
if (is_numeric($_POST[‘tsp_height’])) {
update_option(‘WP_Thumbshot_Preview_Height’, $_POST[‘tsp_height’]);
}
$msg = ‘Settings saved.';
} else {
$msg = ‘Please enter a valid user id.';
}
}
// Refresh options
$this->Populate_Settings();
}
// Display the page HTML, kinda dirty but quick!
echo ‘
Thumbshot Preview Settings
‘.(!is_null($msg) ? ‘
‘.$msg.’
‘ : null).’
‘;
}[/code]
The validation here isn’t perfect, but it does the job. Considering the simplicity of this plugin it doesn’t really make much sense to spend time making it any more complex than this. It would be nice to use templates for the HTML as well but again as this is a simple plugin it doesn’t really warrant the complexity.
Here is a screen shot of the settings page:
Note: The functions listed here are used as part of the object defined in my previous post.