首先, 我們將主文件latex2html.php
中的__construct
函數(shù)更新為
public function __construct()
{
# INIT the plugin: Hook your callbacks
/**
* The settings
*/
require_once( dirname( __FILE__ ).'/includes/admin_settings.php' );
// Instantiate the admin_setting_class
if ( is_admin() ){
$l2h_settings_page = new l2h_admin_setting_class();
}
}
這里, 我們?cè)?code>includes/admin_settings.php中定義了類l2h_admin_setting_class
. 然后用條件判斷是否實(shí)例化類.
然后, includes/admin_settings.php
的內(nèi)容是
<?php
/**
* The admin_setting_class
*/
if( !class_exists( 'l2h_admin_setting_class' ) )
{
class l2h_admin_setting_class
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'l2h_admin_menu' ) );
add_action( 'admin_init', array( $this, 'l2h_admin_init' ) );
}
/**
* Add admin menu
*/
public function l2h_admin_menu()
{
// This page will under "Settings"
add_options_page(
'LaTeX2HTML Setting Page',
'LaTeX2HTML',
'manage_options',
'latex2html',
array( $this, 'l2h_admin_page')
);
}
/**
* Add admin page
*/
public function l2h_admin_page()
{
if( !current_user_can('manage_options') )
wp_die( __( 'You do not have the sufficient permissions to access this page.' ) );
// Set class property
$this->options = get_option( 'l2h_options' );
include_once( dirname( __FILE__ ).'/admin_page.php' );
}
/**
* Add settings items
*/
public function l2h_admin_init(){
/**
* register_setting( $option_group, $option_name, $sanitize_callback );
* add_settings_section( $id, $title, $callback, $page );
* add_settings_field( $id, $title, $callback, $page, $section, $args );
*/
$this->options=get_option( 'l2h_options' );
// Note that we need this to update the settings
register_setting( 'l2h_setting_page', 'l2h_options' );
/*
* General Settings
*/
add_settings_section(
'l2h_pluginPage_section_general',
__( 'General Settings<hr />', 'val2h' ),
array( $this, 'l2h_settings_general_callback' ),
'l2h_setting_page'
);
add_settings_field(
'enall',
__( 'Use <i>English</i> for names?', 'val2h' ),
array( $this, 'l2h_checkbox_render' ),
'l2h_setting_page',
'l2h_pluginPage_section_general',
array(
'field' => 'enall'
)
);
/*
* MathJax Settings
*/
add_settings_section(
'l2h_pluginPage_section_mathjax',
__( 'MathJax Settings<hr />', 'val2h' ),
array( $this, 'l2h_settings_mathjax_callback' ),
'l2h_setting_page'
);
add_settings_field(
'mathjax_cdn',
__( 'MathJax CDN: ', 'val2h' ),
array( $this, 'l2h_text_render' ),
'l2h_setting_page',
'l2h_pluginPage_section_mathjax',
array(
'field' => 'mathjax_cdn'
)
);
add_settings_field(
'mathjax_config',
__( 'MathJax Configuation: ', 'val2h' ),
array( $this, 'l2h_textarea_render' ),
'l2h_setting_page',
'l2h_pluginPage_section_mathjax',
array(
'field' => 'mathjax_config'
)
);
//YOU CAN ADD MORE FIELDS AND SECTIONS
}
/**
* The renders
*/
public function l2h_checkbox_render( $args ) {
$this->options = get_option( 'l2h_options' );
$field = $args['field'];
$value = $this->options[$field];
?>
<input type="checkbox" name="l2h_options[<?php echo $field; ?>]" id="<?php echo $field; ?>" <?php checked( $value, true ); ?> value="1">
<?php
}
public function l2h_text_render( $args ) {
$this->options = get_option( 'l2h_options' );
$field = $args['field'];
$value = $this->options[$field];
?>
<input type="text" name="l2h_options[<?php echo $field; ?>]" id="<?php echo $field; ?>" size="80" value="<?php echo esc_url( $value ); ?>" />
<?php
}
public function l2h_textarea_render( $args ) {
$this->options = get_option( 'l2h_options' );
$field = $args['field'];
$value = $this->options[$field];
?>
<textarea name="l2h_options[<?php echo $field; ?>]" id="<?php echo $field; ?>" cols="80" rows="8"/><?php echo esc_textarea( $value ); ?></textarea>
<?php
}
/**
* The callbacks
*/
public function l2h_settings_general_callback( ) {
echo __( 'The general setting including two checkboxes, the first one for keep all stuff use English as its language and the second one is designed for speed up the homepage loading, when checked, the MathJax render will only active on Single post.', 'val2h' );
}
}
}
這里, 我主要參考了Creating Options Pages 和 Francis Yaconiello 的教程中 settings.php 將同種類型的域用同一個(gè)函數(shù)(l2h_checkbox_render
, l2h_text_render
, l2h_textarea_render
)來(lái)生成具體的代碼.
最后, 我們真正的頁(yè)面代碼是放在includes/admin_page.php
中的(自己查詢下php@
的作用吧:-) )
<div class='wrap'>
<h1>LaTeX2HTML Setting Page</h1><hr />
<form action='options.php' method='post'>
<?php
// This prints out all hidden setting fields
@settings_fields( 'l2h_setting_page' );
@do_settings_sections( 'l2h_setting_page' );
@submit_button();
?>
</form>
</div>