寄存器设置验证功能无输入

时间:2016-10-12 12:26:12

标签: php wordpress

我一直在为Wordpress开发一个插件,它可以正常工作,直到保存插件后端的设置。

首先是register_setting。这一行是一个挂在' admin_init'中的函数。

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

设置字段是通过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'
                                )
                            );
        }
}

验证功能:

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;
}

我使用var_dump($input)exit();检查某些内容是否通过函数和变量传递,但它返回NULL

我知道问题出在$input变量中,因为当我将$input[$viewport][$position];更改为随机字符串时,字符串会保存到此设置字段中。

0 个答案:

没有答案