A class to build options pages for your WordPress plugins and themes.
Learn more in our wiki.
Install using composer:
composer require hallowichig0/wp-options-page
Or just copy src/class-wp-options-page.php
to your project and include in your code:
require 'path/to/your/class-wp-options-page.php';
Just create an WP_Options_Page
class instance on init
action hook:
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' );
Or create your own derived class:
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;
}
public 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 or later