1

I am trying to create a theme option page in my plugin. I have pasted below the entire code. But it is giving an error :

call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members

class KD_Options {

    public function __construct(){
        $this->register_settings_and_fields();
    }

    public function add_menu_page(){
        add_options_page('Theme Options','Theme Options','administrator',__FILE__, array('KD_Options'),'display_options_page'); 
    }

    public function display_options_page(){
        ?>
        <div class="wrap">
            <h2>My theme Options</h2>
            <form method="post" action="options.php" enctype="multipart/form-data">
                <?php settings_fields('kd_plugin_options');//includes all the hidden fields for security ?>
                <?php do_settings_sections(__FILE__); ?>
            </form>
        </div>
        <?php               
    }

    public function register_settings_and_fields(){
        register_setting('kd_plugin_options','kd_plugin_options');//3rd parameter = optional cb
        //get_option('kd_plugin_options');  
        add_settings_section('kd_main_section', 'Main Settings', array($this, 'kd_main_section_cb'), __FILE__);//id, title of section, cb, which page?
        add_settings_field('kd_banner_heading','Banner Heading', array($this, 'kd_banner_heading_setting'), __FILE__, 'kd_main_section');
        add_settings_field('kd_logo','Your Logo: ', array($this, 'kd_logo_setting'), __FILE__, 'kd_main_section');
    }

    /*
     *
     * Inputs
     *
     */

     // Banner Heading
     public function kd_banner_heading_setting() {
         echo '<input>';
     }

     // Logo
     public function kd_logo_setting() {
         echo '<input type="file">';
     }

     //Function for validation and sanitization
     public function kd_main_section_cb(){
         //optional
     }

}

add_action('admin_menu', function(){
    KD_Options::add_menu_page();
});

add_action('admin_init', function(){
    new KD_Options();
});

I guess there is some issue with add_settings_section where the kd_main_section_cb is not getting called or something. I am having some trouble debugging the script. So, if anyone can help me out by letting me know what is the issue.

2

The parameters are fairly wrong here:

add_options_page(
    'Theme Options',
    'Theme Options',
    'administrator',
    __FILE__, 
    array('KD_Options'),
    'display_options_page'
); 

Because you are within a Class, the callback needs to be array( $this, 'display_options_page' ) and not just a function name. You use that array format for WP callbacks within classes so that they are correctly referenced.

However, add_options_page takes 5 parameters, not 6, and they should be:

add_options_page(
    $page_title, 
    $menu_title, 
    $capability, 
    $menu_slug, 
    $function
);

So something like this should work:

add_options_page(
    'Theme Options',
    'Theme Options',
    'administrator',
    'wpse_233388_slug',
    array( $this, 'display_options_page' )
); 

You might be better off using a real capability rather than a role name for your capability too. That way other roles can be allowed the capability without making the users concerned administrators.

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.