Got this working the other day in a single-file php plugin which only consisted of functions.
Now I am trying it out with the WordPress Plugin Boilerplate which is an OOP approach with a lot of different classes.
In short, I am looking at two different classes/files
includes/class-ebay-keyword-suggest.php
and admin/class-ebay-keyword-suggest-admin.php
The first being the core plugin class, and the latter being the admin-specific functionality.
First, class-ebay-keyword-suggest.php
__construct()
's
public function __construct() {
if ( defined( 'EBAY_KEYWORD_SUGGEST_VERSION' ) ) {
$this->version = EBAY_KEYWORD_SUGGEST_VERSION;
} else {
$this->version = '1.0.0';
}
$this->plugin_name = 'ebay-keyword-suggest';
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->register_admin_menu();
}
load_dependencies
loads the class-ebay-keyword-suggest-admin.php
file, among others.
ends with defining $this->loader
as such:
$this->loader = new Ebay_Keyword_Suggest_Loader();
define_admin_hooks
loads up my scripts and stylesheets
private function define_admin_hooks() {
$plugin_admin = new Ebay_Keyword_Suggest_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
}
Proceeded by register_admin_menu
:
private function register_admin_menu() {
$plugin_admin = new Ebay_Keyword_Suggest_Admin( $this->get_plugin_name(), $this->get_version() );
add_action( 'admin_init', array( $plugin_admin, 'ebay_keyword_suggest_settings_init' ) );
add_action( 'admin_menu', array( $plugin_admin, 'ebay_keyword_suggest_add_admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $plugin_admin, 'enqueue_localize' ) );
}
ebay_keyword_suggest_settings_init
is where I am registering the option/setting ebay_keyword_suggest_settings
with callbacks
public function ebay_keyword_suggest_settings_init() {
register_setting( 'pluginPage', 'pluginPage', array($this, 'ebay_keyword_suggest_settings'));
add_settings_section(
'ebay_keyword_suggest_pluginPage_section',
__( 'Your section description', '' ),
array($this, 'ebay_keyword_suggest_settings_section_callback'),
'pluginPage'
);
add_settings_field(
'ebay_keyword_suggest_checkbox_field_0',
__( 'Settings field description', '' ),
array($this, 'ebay_keyword_suggest_checkbox_field_0_render'),
'pluginPage',
'ebay_keyword_suggest_pluginPage_section'
);
}
Next in line in the add_action
functions of register_admin_menu
via ebay_keyword_suggest_add_admin_menu
public function ebay_keyword_suggest_add_admin_menu() {
add_options_page( 'eBay Keyword Suggest', 'eBay Keyword Suggest', 'manage_options', 'ebay_keyword_suggest', array($this, 'ebay_keyword_suggest_options_page') );
}
Then we get to this line:
add_action( 'admin_enqueue_scripts', array( $plugin_admin, 'enqueue_localize' ) );
public function enqueue_localize( $hook ) {
$getoption = get_option( 'ebay_keyword_suggest_settings' );
$enabled = $getoption['ebay_keyword_suggest_checkbox_field_0'];
wp_localize_script( $this->plugin_name, 'ekw_script_vars', array( 'enabled' => __($enabled) ) );
}
As you can see the first instance of get_option
is initiated here under the variable $getoption
... doing a var_dump
always returns false
(and of course wp_localize_script
does not work properly as it is not getting the proper option variable.
Getting this working in a single php file was very easy with a combination of add_action
's above the functions and a simple initiation of register_setting( 'pluginPage', 'title_char_count_settings' );
followed by retrieving the setting with just simple get_option( 'title_char_count_settings' );
This OOP approach is a lot more tricky... I have to be close I'm thinking.. where is the hiccup in this?
register_setting( 'pluginPage', 'pluginPage'
- is that secondpluginPage
intended?option_group
andoption_name
they can be the same. Ref: developer.wordpress.org/reference/functions/register_settingget_option()
. Like with the second parameter inregister_setting( 'pluginPage', 'title_char_count_settings' )
..