WordPress插件开发笔记(四):插件设置页面

首先, 我们将主文件latex2html.php中的__construct函数更新为

    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();
      }
    }

这里, 我们在includes/admin_settings.php中定义了类l2h_admin_setting_class. 然后用条件判断是否实例化类.

然后, includes/admin_settings.php的内容是

<?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 PagesFrancis Yaconiello 的教程中 settings.php 将同种类型的域用同一个函数(l2h_checkbox_render, l2h_text_render, l2h_textarea_render)来生成具体的代码.

最后, 我们真正的页面代码是放在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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容