如何为WordPress插件设置页面添加选项卡?

时间:2015-07-19 03:41:37

标签: wordpress wordpress-plugin settings

我已经为我的wordpress插件创建了一个设置页面,如codex(https://codex.wordpress.org/Creating_Options_Pages #Example 2 中所述。我的问题是如何为WP codex上的#Example 2的WordPress插件设置页面添加选项卡选项(选项卡界面)?

2 个答案:

答案 0 :(得分:0)

我在网上找到了这个教程。希望这可以帮助。 http://plugin.michael-simpson.com/?page_id=47

答案 1 :(得分:0)

我已经使用“ Composer Autoload”为插件创建了“设置”页面
但现在,我只分享一个简单的课程,以展示
我的插件的设置页面有两个标签

  1. “ PDF设置选项卡”
  2. “电子邮件设置”标签
if ( ! class_exists( 'Plugin_Name_Settings_Page' ) ) {
    class Plugin_Name_Settings_Page {

        /* Admin Parent Menu item - properties */
        private $page_title;
        private $menu_title;
        private $capability;
        private $menu_slug;
        private $function;
        private $icon_url;
        private $position;

        public $page_menu;

        public function __construct() {
            $this->page_title = 'Plugin_Name Settings';
            $this->menu_title = 'Plugin_Name Settings';
            $this->capability =  'manage_options'; //'edit_pages';
            $this->menu_slug  =  'Plugin_Name-settings-page'; //'Plugin_Name-admin-dashboard';
            $this->function   = array( $this, 'page' );
            $this->icon_url   = 'dashicons-cart';
            $this->position   = 24;
            
            // Create Admin Page & Menu
            add_action( 'admin_menu', array( $this, 'CreatePageMenu' ), 10 );
            
            add_action( 'Plugin_Name_settings_page_created', array( $this, 'Plugin_Name_pdf_settings_init' ), 10 );
        }

        public function CreatePageMenu() {

            $this->page_menu = add_menu_page(
                $this->page_title,
                $this->menu_title,
                $this->capability,
                $this->menu_slug,
                $this->function,
                $this->icon_url,
                $this->position,
            );

            do_action( 'Plugin_Name_settings_page_created' );
        }

        public function page() {
        ?>
        
        <div class="wrap">
        
            <!-- Create a header in the default WordPress 'wrap' container -->
            <div id="icon-themes" class="icon32"></div>
            <h2>Plugin_Name Settings</h2>
        
            <?php settings_errors(); ?>
        
            <?php
            if ( isset( $_GET ) ) {
                $active_tab = $_GET['tab'];
            } 
            ?>
        
            <h2 class="nav-tab-wrapper">
                <a href="?page=<?php echo $_GET['page']; ?>&tab=pdf_settings" class="nav-tab <?php echo $active_tab == 'pdf_settings' ? 'nav-tab-active' : ''; ?>">PDF Settings</a>
                <a href="?page=<?php echo $_GET['page']; ?>&tab=email_settings" class="nav-tab <?php echo $active_tab == 'email_settings' ? 'nav-tab-active' : ''; ?>">Email Settings</a>
            </h2>
        
        
            <form method="post" action="options.php">
        
            <?php
            if ( $active_tab == 'pdf_settings' ) {
                // settings options group name passed
                settings_fields( 'Plugin_Name_pdf_settings_options_group' );
                // settings options name passed
                do_settings_sections( 'Plugin_Name_pdf_settings_options' );
                submit_button(); 
            } elseif ( $active_tab == 'email_settings' ) {
                // settings options group name passed
                settings_fields( 'Plugin_Name_email_settings_options_group' );
                // settings options name passed
                do_settings_sections( 'Plugin_Name_email_settings_options' );
                submit_button(); 
            }
            
            ?>
            </form>
        </div><!-- /.wrap -->
    
        <?php

        }
        
        
        /***** Start - Tab 01 - PDF Settings Script ***/
        public function plugin_name_pdf_settings_init() {

            
            register_setting(
                'plugin_name_pdf_settings_options_group', // option_group
                'plugin_name_pdf_settings_options', // option_name
                array( $this, 'plugin_name_pdf_settings_options_sanitize' ), // sanitize_callback
                
            );
            
            add_settings_section(
                'plugin_name_pdf_settings_options_section', // id
                'plugin_name PDF Settings', // title
                array( $this, 'plugin_name_pdf_settings_options_section_info' ), // callback
                //'plugin_name-admin-dashboard', //$_GET['page'], // page
                'plugin_name_pdf_settings_options'
            );

            add_settings_field(
                'plugin_name_enrollment_agent_name_option', // id
                'Enrollment Agent Name', // title
                array( $this, 'plugin_name_enrollment_agent_name_option_callback' ), // callback
                'plugin_name_pdf_settings_options', // page
                'plugin_name_pdf_settings_options_section' // section
            );
            
            add_settings_field(
                'plugin_name_enrollment_agent_cert_option', // id
                'Enrollment Agent Cert #', // title
                array( $this, 'plugin_name_enrollment_agent_cert_option_callback' ), // callback
                'plugin_name_pdf_settings_options', // page
                'plugin_name_pdf_settings_options_section' // section
            );
            
            add_settings_field(
                'plugin_name_enrollment_accepted_by_option', // id
                'Enrollment Accepted By', // title
                array( $this, 'plugin_name_enrollment_accepted_by_option_callback' ), // callback
                'plugin_name_pdf_settings_options', // page
                'plugin_name_pdf_settings_options_section' // section
            );

        }

        public function plugin_name_pdf_settings_options_section_info() {
            echo 'plugin_name PDF Settings, admin can set fix daat for PDF.';
        }

        public function plugin_name_pdf_settings_options_sanitize( $input ) {
            $sanitary_values = array();
            if ( isset( $input['plugin_name_enrollment_agent_name_option'] ) ) {
                $sanitary_values['plugin_name_enrollment_agent_name_option'] = $input['plugin_name_enrollment_agent_name_option'];
            }
            if ( isset( $input['plugin_name_enrollment_agent_cert_option'] ) ) {
                $sanitary_values['plugin_name_enrollment_agent_cert_option'] = $input['plugin_name_enrollment_agent_cert_option'];
            }
            if ( isset( $input['plugin_name_enrollment_accepted_by_option'] ) ) {
                $sanitary_values['plugin_name_enrollment_accepted_by_option'] = $input['plugin_name_enrollment_accepted_by_option'];
            }
            return $sanitary_values;
        }

        function plugin_name_enrollment_agent_name_option_callback() {
            $plugin_name_pdf_option_agent_name_value = ( isset($this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_name_option'] ) ) ? $this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_name_option']: '';
        ?>
            <input type="text" name="plugin_name_pdf_settings_options[plugin_name_enrollment_agent_name_option]" value="<?php echo $plugin_name_pdf_option_agent_name_value; ?>">
        <?php
        }

        function plugin_name_enrollment_agent_cert_option_callback() {
            $plugin_name_pdf_option_agent_cert_value = ( isset($this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_cert_option'] ) ) ? $this->plugin_name_pdf_settings_options['plugin_name_enrollment_agent_cert_option']: '';
        ?>
            <input type="text" name="plugin_name_pdf_settings_options[plugin_name_enrollment_agent_cert_option]" value="<?php echo $plugin_name_pdf_option_agent_cert_value; ?>">
        <?php
        }

        function plugin_name_enrollment_accepted_by_option_callback() {
            $plugin_name_pdf_option_accepted_by_value = ( isset($this->plugin_name_pdf_settings_options['plugin_name_enrollment_accepted_by_option'] ) ) ? $this->plugin_name_pdf_settings_options['plugin_name_enrollment_accepted_by_option']: '';
        ?>
            <input type="text" name="plugin_name_pdf_settings_options[plugin_name_enrollment_accepted_by_option]" value="<?php echo $plugin_name_pdf_option_accepted_by_value; ?>">
        <?php
        }
        /*** End - Tab 01 - PDF Settings Script ***/
        
        /**** STart - Tab 02 - Email Settings  Script ***/
        

        public function plugin_name_email_settings_init() {

            
            register_setting(
                'plugin_name_email_settings_options_group', // option_group
                'plugin_name_email_settings_options', // option_name
                array( $this, 'plugin_name_email_settings_options_sanitize' ), // sanitize_callback
                
            );
            
            add_settings_section(
                'plugin_name_email_settings_options_section', // id
                'plugin_name email Settings', // title
                array( $this, 'plugin_name_email_settings_options_section_info' ), // callback
                //'plugin_name-admin-dashboard', //$_GET['page'], // page
                'plugin_name_email_settings_options'
            );

            add_settings_field(
                'plugin_name_teacher_name_option', // id
                'Enrollment Agent Name', // title
                array( $this, 'plugin_name_teacher_name_option_callback' ), // callback
                'plugin_name_email_settings_options', // page
                'plugin_name_email_settings_options_section' // section
            );
            
            add_settings_field(
                'plugin_name_teacher_cert_option', // id
                'Enrollment Agent Cert #', // title
                array( $this, 'plugin_name_teacher_cert_option_callback' ), // callback
                'plugin_name_email_settings_options', // page
                'plugin_name_email_settings_options_section' // section
            );
            
            add_settings_field(
                'plugin_name_room_number_option', // id
                'Enrollment Accepted By', // title
                array( $this, 'plugin_name_room_number_option_callback' ), // callback
                'plugin_name_email_settings_options', // page
                'plugin_name_email_settings_options_section' // section
            );

        }

        public function plugin_name_email_settings_options_section_info() {
            echo 'plugin_name email Settings, admin can set fix daat for email.';
        }

        public function plugin_name_email_settings_options_sanitize( $input ) {
            $sanitary_values = array();
            if ( isset( $input['plugin_name_teacher_name_option'] ) ) {
                $sanitary_values['plugin_name_teacher_name_option'] = $input['plugin_name_teacher_name_option'];
            }
            if ( isset( $input['plugin_name_teacher_cert_option'] ) ) {
                $sanitary_values['plugin_name_teacher_cert_option'] = $input['plugin_name_teacher_cert_option'];
            }
            if ( isset( $input['plugin_name_room_number_option'] ) ) {
                $sanitary_values['plugin_name_room_number_option'] = $input['plugin_name_room_number_option'];
            }
            return $sanitary_values;
        }

        function plugin_name_teacher_name_option_callback() {
            $plugin_name_email_option_agent_name_value = ( isset($this->plugin_name_email_settings_options['plugin_name_teacher_name_option'] ) ) ? $this->plugin_name_email_settings_options['plugin_name_teacher_name_option']: '';
        ?>
            <input type="text" name="plugin_name_email_settings_options[plugin_name_teacher_name_option]" value="<?php echo $plugin_name_email_option_agent_name_value; ?>">
        <?php
        }

        function plugin_name_teacher_cert_option_callback() {
            $plugin_name_email_option_agent_cert_value = ( isset($this->plugin_name_email_settings_options['plugin_name_teacher_cert_option'] ) ) ? $this->plugin_name_email_settings_options['plugin_name_teacher_cert_option']: '';
        ?>
            <input type="text" name="plugin_name_email_settings_options[plugin_name_teacher_cert_option]" value="<?php echo $plugin_name_email_option_agent_cert_value; ?>">
        <?php
        }

        function plugin_name_room_number_option_callback() {
            $plugin_name_email_option_accepted_by_value = ( isset($this->plugin_name_email_settings_options['plugin_name_room_number_option'] ) ) ? $this->plugin_name_email_settings_options['plugin_name_room_number_option']: '';
        ?>
            <input type="text" name="plugin_name_email_settings_options[plugin_name_room_number_option]" value="<?php echo $plugin_name_email_option_accepted_by_value; ?>">
        <?php
        }
        /**** End - Tab 02 - Email Settings Script *****/
        

    }
    
    // Class Instence / object call for execution on requiring this page
    new Plugin_Name_Settings_Page();
}