Plugin Options Won’t Get Saved

I have been trying to create and save custom plugin options. Database entries are getting created but their values won’t get saved. So I need to find which prevents the option values not get saved. In my actual code, there are thousands of lines of code so I have tried to simplify it like below.

Admin.PHP:

<?php

// Preventing Direct Access
defined(‘ABSPATH’) or die(‘You are not allowed here!’);

if (!class_exists(‘MY_Admin’)) {

class MY_Admin {

public function __construct () {

$this->prepare_plugin_admin();

}

private function prepare_plugin_admin () {

$this->set_plugin_links();

$this->set_admin_menu_pages();
$this->set_admin_submenu_pages();

$this->set_admin_settings();
$this->set_admin_settings_sections();
$this->set_admin_settings_fields();

}

// To set plugin links which are displayed on the installed plugins page
private function set_plugin_links () {

$links = [
[
‘url’ => admin_url(‘admin.php?page=mypage1’),
‘text’ => __(‘My Plugin’, MY_TEXTDOM)
],
];

MY_Admin_API::hook_plugin_links($links);

}

private function set_admin_menu_pages () {

$pages = [
[
‘page_title’ => __(‘MY Plugin’, MY_TEXTDOM),
‘menu_title’ => __(‘MY Plugin’, MY_TEXTDOM),
‘capability’ => ‘manage_options’,
‘menu_slug’ => ‘mypage1’,
‘callback’ => [$this, ‘my_settings_page’],
‘icon_url’ => ‘dashicons-format-video’,
‘position’ => null
]
];

MY_Admin_API::hook_admin_menu_pages($pages);

}

private function set_admin_submenu_pages() {

$pages = [
[
‘parent_slug’ => ‘mypage1’,
‘page_title’ => __(‘MY Page 1’, MY_TEXTDOM),
‘menu_title’ => __(‘MY Page 1’, MY_TEXTDOM),
‘capability’ => ‘manage_options’,
‘menu_slug’ => ‘mypage1’,
‘callback’ => [$this, ‘my_settings_page’]
],
[
‘parent_slug’ => ‘mypage1’,
‘page_title’ => __(‘MY Page 2’, MY_TEXTDOM),
‘menu_title’ => __(‘MY Page 2’, MY_TEXTDOM),
‘capability’ => ‘manage_options’,
‘menu_slug’ => ‘mypage2’,
‘callback’ => [$this, ‘my_settings_page’]
],
[
‘parent_slug’ => ‘mypage1’,
‘page_title’ => __(‘MY Page 3’, MY_TEXTDOM),
‘menu_title’ => __(‘MY Page 3’, MY_TEXTDOM),
‘capability’ => ‘manage_options’,
‘menu_slug’ => ‘mypage3’,
‘callback’ => [$this, ‘my_settings_page’]
]
];

MY_Admin_API::hook_admin_submenu_pages($pages);
}

private function set_admin_settings_sections () {

$args = [
[
‘id’ => ‘mypage2_section’,
‘title’ => __(‘My Page 2 Section’, MY_TEXTDOM),
‘callback’ => [$this, ‘mypage2_section_callback’],
‘page’ => ‘mypage2’
],
[
‘id’ => ‘mypage3_section’,
‘title’ => __(‘MY Page 3 Section’, MY_TEXTDOM),
‘callback’ => [$this, ‘mypage3_section_callback’],
‘page’ => ‘mypage3’
],
[
‘id’ => ‘mypage1_section’,
‘title’ => __(‘MY Page 1 Section’, MY_TEXTDOM),
‘callback’ => [$this, ‘mypage1_section_callback’],
‘page’ => ‘mypage1’
]
];

MY_Admin_API::hook_admin_settings_sections($args);

}

// I will only put a single field for one page to make it simple
private function set_admin_settings_fields () {

$args = [
[
‘id’ => ‘my_field’,
‘title’ => __(‘MY Field’, MY_TEXTDOM),
‘callback’ => function ($args) {
MY_Admin_API::checbox_callback($args);
},
‘page’ => ‘mypage3’,
‘section’ => ‘mypage3_section’,
‘args’ => [
‘label_for’ => ‘my_field’,
‘class’ => ‘my-class’
]
]
];

MY_Admin_API::hook_admin_settings_fields($args);

}

private function set_admin_settings () {

$args = [
[
‘option_group’ => ‘my_plugin_options’,
‘option_name’ => ‘my_field’,
‘args’ => [
‘type’ => ‘boolean’,
‘description’ => ‘my_field’,
‘sanitize_callback’ => function ($input) {
MY_Admin_API::sanitize_checkbox($input);
},
‘show_in_rest’ => false,
‘default’ => false
]
],

];

MY_Admin_API::hook_admin_settings($args);

}

Admin API:

<?php

// Preventing Direct Access
defined(‘ABSPATH’) or die(‘You are not allowed here!’);

if (!class_exists(‘WPVJS_Admin_API’)) {

abstract class MY_Admin_API {

private static $plugin_links = [];
private static $auth_capability = ‘manage_options’;

/**
* Get Authorized User Capability
*/

public static final function get_auth_capability () {

return self::$auth_capability;

}

/**
* Set Authorized User Capability
*/

public static final function set_auth_capability (string $capability) {

self::$auth_capability = $capability;

}

/**
* Hook Plugin Links
*/

public static final function hook_plugin_links (array $plugin_links) {

self::$plugin_links = $plugin_links;

if (!empty(self::$plugin_links)) add_filter(‘plugin_action_links_’ . PLUGIN_NAME, [__CLASS__, ‘add_plugin_links’]);

}

/**
* Add Plugin Links
*/

public static final function add_plugin_links ($links) {

foreach (self::$plugin_links as $plugin_link) {

$links[] = ‘<a href=”‘ . $plugin_link[‘url’] . ‘”>’ . $plugin_link[‘text’] . ‘</a> ‘;

}

return $links;

}

/**
* Hook Admin Menu Pages With Optional Scripts
*/

public static final function hook_admin_menu_pages (array $menu_pages, array $page_stylesheets = [], array $page_scripts = []) {

if (!empty($menu_pages)) {

add_action(‘admin_menu’, function () use ($menu_pages, $page_stylesheets, $page_scripts) {

self::add_admin_menu_pages($menu_pages, $page_stylesheets, $page_scripts);

});

}

}

/**
* Add Admin Menu Pages With Optional Scripts
*/

private static final function add_admin_menu_pages (array $admin_menu_pages, array $page_stylesheets, array $page_scripts) {

if (current_user_can(self::$auth_capability)) {

for ($i = 0; $i < count($admin_menu_pages); $i++) {

$page = add_menu_page(
$admin_menu_pages[$i][‘page_title’],
$admin_menu_pages[$i][‘menu_title’],
$admin_menu_pages[$i][‘capability’],
$admin_menu_pages[$i][‘menu_slug’],
$admin_menu_pages[$i][‘callback’],
$admin_menu_pages[$i][‘icon_url’],
$admin_menu_pages[$i][‘position’]
);

if (!empty($page_stylesheets[$i])) self::hook_admin_style($page_stylesheets, $i, $page);

if (!empty($page_scripts[$i])) self::hook_admin_script($page_scripts, $i, $page);

} // Loop

} // Auth

}

/**
* Hook Admin Sub-Menu Pages With Optional Scripts
*/

public static final function hook_admin_submenu_pages (array $sub_pages, array $page_stylesheets = [], array $page_scripts = []) {

if (!empty($sub_pages)) {

add_action(‘admin_menu’, function () use ($sub_pages, $page_stylesheets, $page_scripts) {

self::add_admin_submenu_pages($sub_pages, $page_stylesheets, $page_scripts);

});

}

}

/**
* Add Admin Sub-Menu Pages With Optional Scripts
*/

private static final function add_admin_submenu_pages (array $admin_submenu_pages, array $page_stylesheets, array $page_scripts) {

if (current_user_can(self::$auth_capability)) {

for ($i = 0; $i < count($admin_submenu_pages); $i++) {

$page = add_submenu_page(
$admin_submenu_pages[$i][‘parent_slug’],
$admin_submenu_pages[$i][‘page_title’],
$admin_submenu_pages[$i][‘menu_title’],
$admin_submenu_pages[$i][‘capability’],
$admin_submenu_pages[$i][‘menu_slug’],
$admin_submenu_pages[$i][‘callback’]
);

if (!empty($page_stylesheets[$i])) self::hook_admin_style($page_stylesheets, $i, $page);

}

}

}

/**
* Hook Admin Settings
*/

public static final function hook_admin_settings ($admin_settings) {

if(!empty($admin_settings)) {

add_action(‘admin_init’, function () use ($admin_settings) {

self::add_admin_settings_fields($admin_settings);

});

}

}

/**
* Hook Admin Settings Sections
*/

public static final function hook_admin_settings_sections ($admin_settings_sections) {

if(!empty($admin_settings_sections)) {

add_action(‘admin_init’, function () use ($admin_settings_sections) {

self::add_admin_settings_fields(NULL, $admin_settings_sections, NULL);

});

}

}

/**
* Hook Admin Settings Fields
*/

public static final function hook_admin_settings_fields ($admin_settings_fields) {

if(!empty($admin_settings_fields)) {

add_action(‘admin_init’, function () use ($admin_settings_fields) {

self::add_admin_settings_fields(NULL, NULL, $admin_settings_fields);

});

}

}

/**
* Add All Admin Setting Sections and Fields
*/
private static final function add_admin_settings_fields ($admin_settings = NULL, $admin_settings_sections = NULL, $admin_settings_fields = NULL) {

if (current_user_can(self::$auth_capability)) {

if (!empty($admin_settings)) {

// Register The Setting
foreach ($admin_settings as $setting) {

register_setting(
$setting[‘option_group’],
$setting[‘option_name’],
$setting[‘args’] ?? ”
);

}

}

if (!empty($admin_settings_sections)) {

// Add The Setting Seciton
foreach ($admin_settings_sections as $section) {

add_settings_section(
$section[‘id’],
$section[‘title’],
$section[‘callback’] ?? ”,
$section[‘page’]
);

}
}

if (!empty($admin_settings_fields)) {

// Add Settings Field
foreach ($admin_settings_fields as $field) {

add_settings_field (
$field[‘id’],
$field[‘title’],
$field[‘callback’] ?? ”,
$field[‘page’],
$field[‘section’],
$field[‘args’] ?? ”
);

}
}

}

}

public static function checkbox_callback (array $args) {

$option_name = $args[‘label_for’];

$checkbox = get_option($option_name);

$class = $args[‘class’] ?? ”;

echo ‘<input type=”checkbox” name=”‘ . esc_attr($option_name) . ‘” value=”1″ class=”‘ . sanitize_html_class($class) . ‘” ‘ . ($checkbox ? ‘checked’ : ”) . ‘>’;

}

public static function sanitize_checkbox ($input) {

return isset($input) ? true : false;

}

Settings Page:

<?php

// Preventing Direct Access
defined(‘ABSPATH’) or die(‘You are not allowed here!’);

?>
<div class=”wrap”>

<?php
settings_errors();
// Getting the current active page
$active_screen = esc_attr(get_current_screen()->base);
?>
<!– These tabs are only for quick navigation between subpages –>
<h2 class=”nav-tab-wrapper”>
<a href=”?page=mypage1&tab=mytab1″ class=”nav-tab <?php echo $active_screen == ‘toplevel_page_mypage1’ ? ‘nav-tab-active’ : ” ?>”><?php echo esc_attr__(‘My Page 1’, MY_TEXTDOM) ?></a>
<a href=”?page=mypage2&tab=mytab2″ class=”nav-tab <?php echo $active_screen == ‘my-plugin_page_mypage2’ ? ‘nav-tab-active’ : ” ?>”><?php echo esc_attr__(‘My Page 2’, MY_TEXTDOM) ?></a>
<a href=”?page=mypage3&tab=mytab3″ class=”nav-tab <?php echo $active_screen == ‘my-plugin_page_mypage3’ ? ‘nav-tab-active’ : ” ?>”><?php echo esc_attr__(‘My Page 3’, MY_TEXTDOM) ?></a>
</h2>

<form method=”POST” action=”options.php”>

<?php

switch ($active_screen) {

case ‘my_plugin_page_mypage2’:
settings_fields(‘my_plugin_options’);
do_settings_sections(‘mypage2’);
submit_button();
break;

case ‘my_plugin_page_mypage3’:
settings_fields(‘my_plugin_options’);
do_settings_sections(‘mypage3’);
submit_button();
break;

// This page only has usage documentation that’s why there is no submit button
case ‘toplevel_page_mypage1’:
default:
do_settings_sections(‘mypage1’);
break;
}

?>

</form>

</div>

PS: I have changed almost everything to make it simple for this question. So there might be some minor errors with some stuff.

Read more here:: Plugin Options Won’t Get Saved

Leave a Reply

Your email address will not be published. Required fields are marked *