admin管理员组文章数量:1395225
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?
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?
Share Improve this question edited Feb 15, 2020 at 17:17 Brian Bruman asked Feb 15, 2020 at 17:11 Brian BrumanBrian Bruman 1251 silver badge10 bronze badges 4 |1 Answer
Reset to default 1The second parameter for register_setting()
is the database option name which you would use with get_option()
just as you've done successfully with the title_char_count_settings
option: register_setting( 'pluginPage', 'title_char_count_settings' )
and get_option( 'title_char_count_settings' );
.
So I'm guessing that you either:
Made a typo in the
register_setting()
call in yourebay_keyword_suggest_settings_init()
function:// You used this: register_setting( 'pluginPage', 'pluginPage', array($this, 'ebay_keyword_suggest_settings')); // But perhaps you meant this? (Note the option name - the second parameter) register_setting( 'pluginPage', 'ebay_keyword_suggest_settings', array($this, 'ebay_keyword_suggest_settings'));
Or you made a typo in your
get_option()
call in yourenqueue_localize()
function, where you should have usedget_option( 'pluginPage' )
. But it's unlikely that you would usepluginPage
as the name for that option? :)
本文标签: pluginsgetoptionwplocalizescript Not Working in OOP Plug In
版权声明:本文标题:plugins - get_optionwp_localize_script Not Working in OOP Plug In 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744745418a2622846.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
register_setting( 'pluginPage', 'pluginPage'
- is that secondpluginPage
intended? – Sally CJ Commented Feb 15, 2020 at 17:50option_group
andoption_name
they can be the same. Ref: developer.wordpress/reference/functions/register_setting – Brian Bruman Commented Feb 15, 2020 at 17:51get_option()
. Like with the second parameter inregister_setting( 'pluginPage', 'title_char_count_settings' )
.. – Sally CJ Commented Feb 15, 2020 at 17:55