luizbills / wp-options-page
在您的WordPress插件和主题中构建选项/设置页面的简单方法。
0.7.0
2023-12-14 20:02 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpstan/phpstan: ^1.10
- szepeviktor/phpstan-wordpress: ^1.1
This package is auto-updated.
Last update: 2024-09-14 21:31:09 UTC
README
一个用于为您的WordPress插件和主题构建选项页面的类。
文档
在我们的 wiki 中了解更多信息。
安装
使用Composer安装
composer require luizbills/wp-options-page
入门
只需在 init
动作钩子中创建一个 WP_Options_Page
类实例
function yourprefix_create_settings_page () { $page = new WP_Options_Page(); // give your page a ID $page->id = 'my_settings_page'; // set the menu name $page->menu_title = 'My Settings'; // register your options fields $page->fields = [ // a simple text input field [ 'id' => 'api_key', 'title' => 'API Key', 'type' => 'text', ] ]; // register the page $page->init(); // access the stored options $api_key = $page->get_option( 'api_key' ); // store this page in a global object or variable // So you can easily your instance class later // example: My_Plugin->settings = $page; } add_action( 'init', 'yourprefix_create_settings_page' );
或者创建您自己的派生类
class My_Settings_Page extends WP_Options_Page { // I recommend using Singleton pattern // So you can easily retrieve the class later // example: My_Settings_Page::instance()->get_option( 'api_key' ); private static $instance = null; public static function instance () { if ( ! self::$instance ) self::$instance = new self(); return self::$instance; } private function __construct () { add_action( 'init', [ $this, 'init' ] ); } // overrides the `init` method to setup your page public function init () { // give your page a ID $this->id = 'my_settings_page'; // set the menu name $this->menu_title = 'My Settings'; // register the page parent::init(); } // overrides the `get_fields` method to register your fields public function get_fields () { return [ [ 'id' => 'api_key', 'title' => 'API Key', 'type' => 'text', ] ]; } } // start your class My_Settings_Page::instance();
预览
此外,您还可以安装并研究我们的 演示插件。
许可证
GPLv2或更高版本