0

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?

  • You used register_setting( 'pluginPage', 'pluginPage' - is that second pluginPage intended? – Sally CJ Feb 15 '20 at 17:50
  • Yeah... it's just option_group and option_name they can be the same. Ref: developer.wordpress.org/reference/functions/register_setting – Brian Bruman Feb 15 '20 at 17:51
  • 1
    Yes, but it's the name of the database option, which you should use with get_option(). Like with the second parameter in register_setting( 'pluginPage', 'title_char_count_settings' ) .. – Sally CJ Feb 15 '20 at 17:55
  • 1
    Yes, wow, that's exactly right! Completely overlooked on my part. Changed as noted and now it works. Thank you. If you want to add an answer I can accept it so this can be closed :) – Brian Bruman Feb 15 '20 at 19:56
1

The 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:

  1. Made a typo in the register_setting() call in your ebay_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'));
    
  2. Or you made a typo in your get_option() call in your enqueue_localize() function, where you should have used get_option( 'pluginPage' ). But it's unlikely that you would use pluginPage as the name for that option? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.