0

I've been developing a plugin for Wordpress, which works fine until saving the settings in the plugin's backend.

First, the register_setting. This line is in a function which is hooked in the 'admin_init'.

register_setting('dfp_banners_options', 'dfp_banners_options', 'dfp_options_validate');

The settings field are generated through a foreach;

add_action('admin_init', 'dfp_banners_settings');
function dfp_banners_settings() {
    $viewports = array("default", "tablet", "tablet_phone", "phone");

        foreach($viewports as $viewport) {

            add_settings_field(
                                'dfp_viewport_above-main-content_' . $viewport, 
                                'Above Main Content', 
                                'dfp_viewport_dimension_callback', 
                                'dfp_plugin_sections_' . $viewport, 
                                'plugin_viewport_' . $viewport,
                                array(
                                    "viewport" => $viewport,
                                    "position" => 'above-main-content'
                                )
                            );

            add_settings_field(
                                'dfp_viewport_below-main-content_' . $viewport, 
                                'Below Main Content', 
                                'dfp_viewport_dimension_callback', 
                                'dfp_plugin_sections_' . $viewport, 
                                'plugin_viewport_' . $viewport,
                                array(
                                    "viewport" => $viewport,
                                    "position" => 'below-main-content'
                                )
                            );

            add_settings_field(
                                'dfp_viewport_below-entry-content_' . $viewport, 
                                'Below Entry Content', 
                                'dfp_viewport_dimension_callback', 
                                'dfp_plugin_sections_' . $viewport, 
                                'plugin_viewport_' . $viewport,
                                array(
                                    "viewport" => $viewport,
                                    "position" => 'below-entry-content'
                                )
                            );

            add_settings_field(
                                'dfp_viewport_sidebar-widget_' . $viewport, 
                                'Sidebar Widget', 
                                'dfp_viewport_dimension_callback', 
                                'dfp_plugin_sections_' . $viewport, 
                                'plugin_viewport_' . $viewport,
                                array(
                                    "viewport" => $viewport,
                                    "position" => 'sidebar-widget'
                                )
                            );

            add_settings_field(
                                'dfp_viewport_above-footer_' . $viewport, 
                                'Above Footer', 
                                'dfp_viewport_dimension_callback', 
                                'dfp_plugin_sections_' . $viewport, 
                                'plugin_viewport_' . $viewport,
                                array(
                                    "viewport" => $viewport,
                                    "position" => 'above-footer'
                                )
                            );

            add_settings_field(
                                'dfp_viewport_floor-ad_' . $viewport, 
                                'Floor Ad', 
                                'dfp_viewport_dimension_callback', 
                                'dfp_plugin_sections_' . $viewport, 
                                'plugin_viewport_' . $viewport,
                                array(
                                    "viewport" => $viewport,
                                    "position" => 'floor-ad'
                                )
                            );
        }
}

The validate function:

function dfp_options_validate($input) { 

    $options = get_option('dfp_banners_options');

    $viewports = array("default", "tablet", "tablet_phone", "phone");

    $positions = array("above-main-content", "below-main-content", "below-entry-content", "sidebar-widget", "above-footer", "floor-ad");

    foreach($viewports as $viewport) {
        foreach($positions as $position) {
            $options[$viewport][$position] = $input[$viewport][$position];
        }
    }

    return $options;
}

I use var_dump($input) and exit(); to check if something gets passed through the function and the variable, but it returns NULL.

I know the problem lies within the $input variable, because when I change $input[$viewport][$position]; to a random string, the string gets saved into this settings field.

Azylak
  • 157
  • 2
  • 15
  • Then `$input[$viewport][$position]` must not be defined. – Jay Blanchard Oct 12 '16 at 12:28
  • @JayBlanchard I have no clue what you mean, the `$input` should contain all the inputdata from the plugin form, so its necessary. My problem won't solve when removing that part of the code. – Azylak Oct 12 '16 at 12:43
  • The problem you're having is because that variable has not data in it or does not exist. You have to make sure `$input[$viewport][$position]` has value and exists. – Jay Blanchard Oct 12 '16 at 12:55
  • With all due respect, I already know that. The problem is why Wordpress wont send data to this validation function, which should fill `$input` with data, when saving the plugin form in Wordpress. To summarize, I know $input has no data, the problem is why has it no data. It seems I have some problems explaining this, English isn't my native language, but eventhough its related to PHP, I don't think the problem is PHP related, but more Wordpress related. Can you figure out what I mean? :) – Azylak Oct 12 '16 at 13:39

0 Answers0