0

I am developing my first plugin, and I have an options page setup. Now, I am trying to add some fields.

I have this in xxx-admin.php in admin folder

public function section_callback() {
        echo '<p>Select which areas of content you wish to display.</p>';
    }

    public function field_callback() {
        $html = '<input type="text" id="input_offer" name="questionaire[input_field]" />'; 
        echo $html; 
    }

    public function register_settings() {
        // First, we register a section. This is necessary since all future options must belong to a 
        add_settings_section(
            'my_settings_section',         // ID used to identify this section and with which to register options
            'iPhones',                  // Title to be displayed on the administration page
            'section_callback', // Callback used to render the description of the section
            $this->plugin_name                           // Page on which to add this section of options
        );

        // Next, we'll introduce the fields for toggling the visibility of content elements.
        add_settings_field( 
            'input_field',                      // ID used to identify the field throughout the theme
            'Offer',                           // The label to the left of the option interface element
            'field_callback',   // The name of the function responsible for rendering the option interface
            $this->plugin_name,                          // The page on which this option will be displayed
            'my_settings_section'         // The name of the section to which this field belongs
        );

        register_setting( $this->plugin_name, 'input_field' );
    }

In the main class-xxx.php file in the includes folder I have:

private function define_admin_hooks() {

        $plugin_admin = new Questionaire_Admin( $this->get_plugin_name(), $this->get_version() );

    ...

        $this->loader->add_action( 'admin_init', $plugin_admin, 'register_settings' );

    ...

    }

But I am getting:

Warning: call_user_func() expects parameter 1 to be a valid callback, function 'section_callback' not found or invalid function name in /Users/eamonwhite/wordpress-plugin-playground/wp-admin/includes/template.php on line 1332

Warning: call_user_func() expects parameter 1 to be a valid callback, function 'field_callback' not found or invalid function name in /Users/eamonwhite/wordpress-plugin-playground/wp-admin/includes/template.php

on line 1378

How do I get it to see the callback functions?

0

I needed something like array($this, 'field_callback') instead of just the string 'field_callback' inside the add_settings_xxx functions. I am still seeking an explanation for this solution if you can enlighten me. Why does this work?

  • 2
    this is how you define a callback which is based on calling a method of an object in php. Nothing specific to wordpress here. – Mark Kaplun Jan 19 '18 at 20:02

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.