Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is my entire code. It doesn't save the data I enter on the plugin settings page. I don't have a clue on why this happens! Can somebody please help me?

<?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>';
}
?>
share|improve this question
    
I think it is a better way you sue the settings API, it is easier and fast forward. You find excellent tutorials in the www –  bueltge Jan 23 '13 at 13:01
    
I am using setting API! Can you help me find what I am doing wrong in this code? –  oDx Jan 23 '13 at 13:08
    
Import on the settings API is the id, the string (like settings-group) there you use to identifier the data. see tutorial –  bueltge Jan 23 '13 at 13:39
    
Please check, if you change the first value on register_settingsto 'RSP_options-group'. You have the same key for group and name. Use this also on settings_fields. –  bueltge Jan 23 '13 at 13:45
    
Do I need to change it somewhere else, because still it doesn't work! :( –  oDx Jan 23 '13 at 13:58

1 Answer 1

up vote 0 down vote accepted

The problem is your validate function RSP_options_validate(). This function always a empty string. Check your conditional statement with preg_match.

If you test your source without this functions, it works.

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

to

register_setting( 'RSP_options_group', 'RSP_options' );

share|improve this answer
    
That was it thanks ! :D –  oDx Jan 23 '13 at 15:59
    
Welcome. Leave always good readable source, makes so much easier to check it. –  bueltge Jan 23 '13 at 16:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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