WordPress 插件开发 如何新建自定义页面?

2021-09-17 创建 6

WordPress 插件开发中,我们需要自定义一些 option 操作选项。下面我们举例说明:在“设置”菜单下 — 添加一个名为“My Settings”的菜单,然后在页面中添加名为 ID Number 、Title 的2个填写框,我们这里尽量使用WordPress的原生代码,没有写很多不必要的HTML代码。

WordPress 插件开发 如何新建自定义页面?

PHP代码实例

wp-content/plugins 目录下新建一个文件夹:wp-jpost 然后将以下文件命名为:plugin.php ,然后记得在插件列表中启用插件。

<?php class MySettingsPage {     /**      * Holds the values to be used in the fields callbacks      */     private $options;      /**      * Start up      */     public function __construct()     {         add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );         add_action( 'admin_init', array( $this, 'page_init' ) );     }      /**      * Add options page      */     public function add_plugin_page()     {         // This page will be under "Settings"         add_options_page(             'Settings Admin',              'My Settings',              'manage_options',              'my-setting-admin',              array( $this, 'create_admin_page' )         );     }      /**      * Options page callback      */     public function create_admin_page()     {         // Set class property         $this->options = get_option( 'my_option_name' );         ?>         <div class="wrap">             <h1>My Settings</h1>             <form method="post" action="options.php">             <?php                 // This prints out all hidden setting fields                 settings_fields( 'my_option_group' );                 do_settings_sections( 'my-setting-admin' );                 submit_button();             ?>             </form>         </div>         <?php     }      /**      * Register and add settings      */     public function page_init()     {                 register_setting(             'my_option_group', // Option group             'my_option_name', // Option name             array( $this, 'sanitize' ) // Sanitize         );          add_settings_section(             'setting_section_id', // ID             'My Custom Settings', // Title             array( $this, 'print_section_info' ), // Callback             'my-setting-admin' // Page         );            add_settings_field(             'id_number', // ID             'ID Number', // Title              array( $this, 'id_number_callback' ), // Callback             'my-setting-admin', // Page             'setting_section_id' // Section                    );                add_settings_field(             'title',              'Title',              array( $this, 'title_callback' ),              'my-setting-admin',              'setting_section_id'         );           }      /**      * Sanitize each setting field as needed      *      * @param array $input Contains all settings fields as array keys      */     public function sanitize( $input )     {         $new_input = array();         if( isset( $input['id_number'] ) )             $new_input['id_number'] = absint( $input['id_number'] );          if( isset( $input['title'] ) )             $new_input['title'] = sanitize_text_field( $input['title'] );          return $new_input;     }      /**       * Print the Section text      */     public function print_section_info()     {         print 'Enter your settings below:';     }      /**       * Get the settings option array and print one of its values      */     public function id_number_callback()     {         printf(             '<input type="text" id="id_number" name="my_option_name[id_number]" value="%s" />',             isset( $this->options['id_number'] ) ? esc_attr( $this->options['id_number']) : ''         );     }      /**       * Get the settings option array and print one of its values      */     public function title_callback()     {         printf(             '<input type="text" id="title" name="my_option_name[title]" value="%s" />',             isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''         );     } }  if( is_admin() )     $my_settings_page = new MySettingsPage();
WordPress 插件开发 如何新建自定义页面?

实例来源WordPress官网:https://codex.wordpress.org/Creating_Options_Pages#Example_.232

腾讯云限时秒杀【点击购买】

搬瓦工,CN2高速线路,1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

阿里云香港、新加坡VPS/1核/1G/25G SSD/1T流量/30M带宽/年付¥288【点击购买】

`微信`扫码 加好友,入群
WordPress插件主题开发定制
Linux服务器配置迁移
扣扣群 665443669

标题:WordPress 插件开发 如何新建自定义页面?

分类:资讯

链接:https://hubjj.com/1497.html