1. Go to this page and download the library: Download luizbills/wp-options-page library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
luizbills / wp-options-page example snippets
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();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.