1

I am in the process of creating some options / the options page for my plugin. In the context of this I have only added two checkboxes so far to store boolean values. The creation, display on the page and saving works fine. Regarding future options, I wanted to test the validation of the options. For this I have created the function "sanitize_options", which now simply returns the parameter it receives as a test.

public function sanitize_options( $data ) {
        return $data;
}

I passed the function name to the function "register_settings" as parameter.

register_setting(
            'faqdesk_general_options', //Group Name
            'faqdesk_general_options', //Name of the option
            array(
                'type'              => 'array',
                'sanitize_callback' => 'sanitize_options',
            )
);

Of course, this doesn't make much sense or serve any purpose. But as I said, I just wanted to try it out. But now I had to notice that this function is not called at all. In addition, the option value is filled with NULL when I save the changes on the page. If I take the value sanitize_callback out of the argument array completely, everything works as desired, so error-free.

What is the reason for this, where is the error? Am I making a stupid thinking error? Please help me out and excuse me if I am simply stupid.

Following the complete class:

<?php

class Faqdesk_Settings {

    /**
     * Single instance of the class
     *
     * @var null
     * @since 1.0.0
     */
    protected static $_instance = null;

    /**
     * Singleton
     *
     * @since 1.0.0
     * @static
     * @return self Main instance
     */
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * Add admin menu.
     *
     * @return void
     */
    public function settings_admin_menu() {

        add_submenu_page(
            'faqdesk_main_menu', //slug name for the parent menu (menu_slug is set add_admin_menu() function in the class Faqdesk_Admin)
            __( 'FAQDesk Settings', 'faqdesk' ), //page title shown in the <title> tag
            __( 'Settings', 'faqdesk' ), //name of the submenu displayed on the Dashboard
            'manage_options', //minimum capability required to view the submenu
            'faqdesk_settings', //slug name of the submenu
            array($this,'render_settings_page_content') //function to be called to display the page content
        );
    }


    /**
     * Renders a page with a tab navigation to display the settings.
     */
    public function render_settings_page_content( $active_tab = '' ) {
        ?>

        <div class="wrap">

            <h2><?php _e( 'FAQDesk Settings', 'faqdesk' ); ?></h2>
            <?php settings_errors(); ?>

            <?php if( isset( $_GET[ 'tab' ] ) ) {
                $active_tab = $_GET[ 'tab' ];
            } else if( $active_tab == 'social_options' ) {
                $active_tab = 'social_options';
            } else if( $active_tab == 'input_examples' ) {
                $active_tab = 'input_examples';
            } else {
                $active_tab = 'general_options';
            } // end if/else ?>

            <h2 class="nav-tab-wrapper">
                <a href="?page=faqdesk_settings&tab=general_options" class="nav-tab <?php echo $active_tab == 'general_options' ? 'nav-tab-active' : ''; ?>"><?php _e( 'General', 'faqdesk' ); ?></a>
            </h2>

            <form method="post" action="options.php">
                <?php

                settings_fields( 'faqdesk_general_options' ); //settings group name - call references the whitelisted option which had declared with register_setting()
                do_settings_sections( 'faqdesk_settings' ); //slug name of the page whos settings section should be outputed, therefore it outputs all the sections and form fields which has defined in initialize_general_options()
                submit_button(__( 'Save general settings', 'faqdesk' ), '');

                ?>
            </form>

        </div><!-- /.wrap -->
    <?php
    }

    /**
     * This function provides a simple description for the General Options page.
     *
     * It's called from the 'initialize_general_options' function by being passed as a parameter
     * in the add_settings_section function.
     */
    public function general_options_callback() {
        $options = get_option('faqdesk_general_options');
        var_dump($options);
        echo '<p>' . __( 'Adjust the settings, which concern basic details and functions of the plugin.', 'faqdesk' ) . '</p>';
    } // end general_options_callback

    /**
     * Initializes the general options page by registering the Sections,
     * Fields, and Settings.
     *
     * This function is registered with the 'admin_init' hook.
     */
    public function initialize_general_options() {

        // Register setting: The group name can be anything actually, but I think its just simpler to name it the same as the option that will get stored in the database.
        register_setting(
            'faqdesk_general_options', //Group Name
            'faqdesk_general_options', //Name of the option
            array(
                'type'              => 'array',
                'sanitize_callback' => 'sanitize_options',
            )
        );

        // Add section, which defines how the settings will be visually grouped 
        add_settings_section(
            'general_settings_section',                     // also HTML ID tag for the section
            __( 'General Options', 'faqdesk' ),             // Title that will show within an <H2> tag
            array( $this, 'general_options_callback'),      // Callback that will echo some explanations about that section
            'faqdesk_settings'                              // slug name of the page which to show the section 
        );

        // Add settings fields, describes how to add the form input
        // SETTING: Cleanup on deactivation?
        add_settings_field(
            'delete_data_on_deactivation',                                  // also HTML ID tag
            __( 'Cleanup on deactivation', 'faqdesk' ),                     // Formatted title of the field, which is displayed as the label for the field on output
            array( $this, 'toggle_delete_data_on_deactivation_callback'),   // Callback function that will echo the form field
            'faqdesk_settings',                                             // The page on which this option will be displayed
            'general_settings_section',                                     // The name of the section to which this field belongs
            array(                                                          // The array of arguments to pass to the callback. In this case, just a description.
                __( 'Activate this setting to delete all data, when the plugin is deactivated.', 'faqdesk' ) 
            )
        );

        // SETTING: Cleanup on deletion?
        add_settings_field(
            'delete_data_on_deletion',                                      // also HTML ID tag
            __( 'Cleanup on deletion', 'faqdesk' ),                         // Formatted title of the field, which is displayed as the label for the field on output
            array( $this, 'toggle_delete_data_on_deletion_callback'),       // Callback function that will echo the form field
            'faqdesk_settings',                                             // The page on which this option will be displayed
            'general_settings_section',                                     // The name of the section to which this field belongs
            array(                                                          // The array of arguments to pass to the callback. In this case, just a description.
                __( 'Activate this setting to delete all data, when the plugin is deleted.', 'faqdesk' ) 
            )
        );


    } // end wppb-demo_initialize_theme_options


    /**
     * This function renders the interface elements for toggling the visibility of the header element.
     *
     * It accepts an array or arguments and expects the first element in the array to be the description
     * to be displayed next to the checkbox.
     */
    public function toggle_delete_data_on_deactivation_callback($args) {

        // First, we read the options collection
        $options = get_option('faqdesk_general_options');

        // Next, we update the name attribute to access this element's ID in the context of the display options array
        // We also access the show_header element of the options collection in the call to the checked() helper function
        $html = '<input type="checkbox" id="delete_data_on_deactivation" name="faqdesk_general_options[delete_data_on_deactivation]" value="1" ' . checked( 1, isset( $options['delete_data_on_deactivation'] ) ? $options['delete_data_on_deactivation'] : 0, false ) . '/>';

        // Here, we'll take the first argument of the array and add it to a label next to the checkbox
        $html .= '<label for="delete_data_on_deactivation">&nbsp;'  . $args[0] . '</label>';

        echo $html;

    } 

    public function toggle_delete_data_on_deletion_callback($args) {

        // First, we read the options collection
        $options = get_option('faqdesk_general_options');
        $delete_on_deactivation = isset( $options['delete_data_on_deactivation'] ) ? $options['delete_data_on_deactivation'] : 0;
        $attribute = '';
        if($delete_on_deactivation == 1) {
            $attribute = ' disabled="disabled" faqdesk-tooltip="' . __( 'Not so useful to set this when already at deactivation all plugin data is deleted...', 'faqdesk' ) . '" ';
        }

        // Next, we update the name attribute to access this element's ID in the context of the display options array
        // We also access the show_header element of the options collection in the call to the checked() helper function
        $html = '<input type="checkbox"' . $attribute . 'id="delete_data_on_deletion" name="faqdesk_general_options[delete_data_on_deletion]" value="1" ' . checked( 1, isset( $options['delete_data_on_deletion'] ) ? $options['delete_data_on_deletion'] : 0, false ) . '/>';

        // Here, we'll take the first argument of the array and add it to a label next to the checkbox
        $html .= '<label for="delete_data_on_deletion">&nbsp;'  . $args[0] . '</label>';

        echo $html;

    } 

    public function sanitize_options( $data ) {
        return $data;
    }

}

1 Answer 1

3

If I take the value sanitize_callback out of the argument array completely, everything works as desired, so error-free.

Yes, and that's because you used the wrong callable syntax which then causes PHP to use a global function named sanitize_options instead of the method/function of the same name in your class (i.e. Faqdesk_Settings::sanitize_options()).

So to solve the problem, in Faqdesk_Settings::initialize_general_options(), just replace the 'sanitize_options' with array( $this, 'sanitize_options' ), like so:

register_setting(
    'faqdesk_general_options', //Group Name
    'faqdesk_general_options', //Name of the option
    array(
        'type'              => 'array',
        'sanitize_callback' => array( $this, 'sanitize_options' ),
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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