我的第一个插件没有将数据保存在选项中

时间:2013-01-23 作者:defiant

这是我的全部代码。它不会保存我在插件设置页面上输入的数据。我不知道为什么会这样!谁能帮帮我吗?

<?php // add the admin options page
add_action(\'admin_menu\', \'plugin_admin_add_page\');
function plugin_admin_add_page() {
    add_options_page(
        \'Revenue Share Plugin\',
        \'RSP Menu\',
        \'manage_options\',
        \'RSP_plugin\',
        \'RSP_options_page\'
    );
}
?>

<?php // display the admin options page
function RSP_options_page() {
    ?>
    <div>
    <h2>Adsense Revenue Share Plugin</h2>
    Set the publication id and the position of the ad here!
    <form action="options.php" method="post">
    <?php settings_fields(\'RSP_options\'); ?>
    <?php do_settings_sections(\'RSP_plugin\'); ?>

    <input name="Submit" type="submit" value="<?php esc_attr_e(\'Save Changes\'); ?>" />
    </form></div>

    <?php 

    $options = get_option(\'RSP_options\');
    echo \'<br /><h4>Current Settings</h4><br>\';
    echo \'Adsense Publisher Id\';
    echo $options[\'RSP_text_string\']; 
    print_r($options)
    ?>

    <?php
}?>

<?php // add the admin settings and such
add_action(\'admin_init\', \'RSP_admin_init\');
function RSP_admin_init(){
    register_setting( \'RSP_options\', \'RSP_options\', \'RSP_options_validate\' );
    add_settings_section(\'RSP_main\', \'Main Settings\', \'RSP_section_text\', \'RSP_plugin\');
    add_settings_field(
        \'RSP_text_string\',
        \'RSP Adsense Pub Id\',
        \'RSP_setting_string\',
        \'RSP_plugin\',
        \'RSP_main\'
    );
}
?>

<?php // validate our options
function RSP_options_validate($input) {
    $options = get_option(\'RSP_options\');
    $options[\'RSP_text_string\'] = trim($input[\'RSP_text_string\']);
    if(!preg_match(\'/^[a-z0-9]{32}$/i\', $options[\'RSP_text_string\'])) {
        $options[\'RSP_text_string\'] = \'\';
    }
    return $options;
}
?>

<?php
function RSP_setting_string() {
    $options = get_option(\'RSP_options\');
    echo "<input id=\'RSP_text_string\' name=\'RSP_options[RSP_text_string]\' size=\'40\' type=\'text\' value=\'{$options[\'RSP_text_string\']}\' />";
}
?>

<?php
function RSP_section_text() {
    echo \'<p>Main description of this section here.</p>\';
}
?>

1 个回复
最合适的回答,由SO网友:bueltge 整理而成

问题是验证函数RSP_options_validate(). 此函数始终为空字符串。检查您的条件语句preg_match.

如果您在没有此函数的情况下测试源代码,它会正常工作。

register_setting( \'RSP_options_group\', \'RSP_options\', \'RSP_options_validate\' );

register_setting( \'RSP_options_group\', \'RSP_options\' );

结束