• Resolved Steven

    (@spstieng)


    Hi.

    I’ve added a new field in my Settings->general page, following this example:https://codex.www.ads-software.com/Settings_API

    The field is added (also in my DB), but any data I input is not stored.
    I’m using both add_settings_field and register_settings:

    add_settings_field('site_logo', 'Logo', 'site_logo_callback_function', 'general', 'default');
    register_setting('general','site_logo');

    So why is data not stored when I click the save button?

Viewing 15 replies - 1 through 15 (of 16 total)
  • Have you checked the options table to see if the value is being saved?

    It’s all well adding the new field, but additionally to that, when you load the page you need to check for the value, and if it’s present, check the box, or fill in field with the saved value, else it’ll just look unset or empty, even when a value exists for the setting.

    Thread Starter Steven

    (@spstieng)

    Yes, there is a new option in the options table called Site_logo, but without any value.

    I don’t think I have to write code to save the value? It should be saved when I click the save button.

    As for populating…. hmm.. yeah, I should probably add code for that ??

    What kind of input are you using, a text field, checkbox, dropdown? etc..

    Have you entered a value or checked the box?

    If you’re saving data but it’s not going to the DB, then something is a miss, and i’d guess it’s because you need to also call..

    settings_fields('yourfield');

    ..when outputting the field..

    https://codex.www.ads-software.com/Creating_Options_Pages#settings_fields_Function
    https://codex.www.ads-software.com/Creating_Options_Pages#See_It_All_Together_2

    I’m not an expert in the settings API, but seeing as this replaces the use of nonces i’d expect the call to settings_fields() to be a requirement in order for WordPress to receive data from the option.

    At least that was the case in some minor plugin code i wrote, where i was simply hooking onto the options page (adding 1 new field, like you’re doing).

    Thread Starter Steven

    (@spstieng)

    This is my code:

    function senior_settings_api_init() {
         // Add the field with the names and function to use for our new settings, put it in our new section
         add_settings_field('site_logo', 'Logo', 'site_logo_callback_function', 'general', 'default');
    
         // Register our setting so that $_POST handling is done for us and our callback function just has to echo the <input>
         register_setting('general','site_logo');
     }
    
     add_action('admin_init', 'senior_settings_api_init');
    
     function site_logo_callback_function() {
    
        echo '<input type="text" class="regular-text code" value="" id="siteLogo" name="siteLogo">';
    
    }

    Currently I’m not populating the extra field on page load. I will have to add that using your suggestion.

    But when inputting data into the text field, and I click save, the data is not stored in database.

    Thread Starter Steven

    (@spstieng)

    Ok, had a look at “See It All Together part 2”. Added settings_fields() (I have no idea what it does), and also added get_option() to output any stored value.

    But it still doesn’t work ??

    This is my updated code

    add_action('admin_init', 'senior_settings_api_init');
    
     function senior_settings_api_init() {
         // Add the field with the names and function to use for our new settings, put it in our new section
         add_settings_field('site_logo', 'Logo', 'site_logo_callback_function', 'general', 'default');
    
         // Register our setting so that $_POST handling is done for us and our callback function just has to echo the <input>
         register_setting('general','site_logo');
     }
    
     function site_logo_callback_function() {
        settings_fields( 'general' );
        echo '<input type="text" class="regular-text code" value="'.get_option('site_logo').'" id="siteLogo" name="siteLogo">';
    }

    Try..

    register_setting( 'mygroup' , 'site_logo' );

    and..

    settings_fields( 'site_logo' );

    https://codex.www.ads-software.com/Function_Reference/register_setting

    You can place your settings field onto one of the options pages as you’ve done here..

    add_settings_field('site_logo', 'Logo', 'site_logo_callback_function', 'general', 'default');

    ..however, this doesn’t mean your registered setting (eg. register_setting) can be appended to an existing group of options, your own registered settings have to use their own group, they can’t use an existing one..
    eg.

    register_setting('general','site_logo');

    I can understand how it could seem that way, but the add_settings_field function is just a convenience thing for adding to existing sections and options pages, however it doesn’t merge your custom options with existing groups of options, custom options are registered and stored seperately…

    Not sure if that came across clear, but i hope you understand what i mean.

    If anyone thinks or knows differently to what i’ve said above, they are welcome to correct me, that is functionality as i understand it to be, so it’s quite possible my understanding is not accurate… all the same, try what i said and see if it helps… ??

    Thread Starter Steven

    (@spstieng)

    So…. what could should I use then?
    I’ve tried what you suggested above, but it’s not working.

    Have a look at the pastebin further up, compare what i did there with what you’re doing.

    If you’re having trouble reaching that page(as i am – server must be down), try the cached version.

    Thread Starter Steven

    (@spstieng)

    Thanks dude ??
    Will compare (again) later.

    I just posted this: https://stackoverflow.com/questions/2585933/not-able-to-store-custom-options-in-wordpress

    For some weird reason, the values are only stored if I call the option field ‘new_option_name’.

    Why is this so hard ??

    Well your input name should match the option you’re registering.

    register_setting( 'cms-options', 'sitelogo' );
    <input type="text" name="new_option_name" value="<?php echo get_option('sitelogo'); ?>" />

    It’s odd that it would work with “new_option_name”. You don’t have another file or plugin active thats running some register setting code, perhaps in testing.. ? Maybe there’s some duplication or conflict between your code above and code elsewhere..

    I’d be inclined to suggest using a different name in each of the relevant places to see if the problem still occurs.

    Eg.

    register_setting( 'cms-options', 'testlogo' );
    <input type="text" name="testlogo" value="<?php echo get_option('testlogo'); ?>" />

    Thread Starter Steven

    (@spstieng)

    Yeah, that was just a typo.

    I’ve tried many different names. The only one that works, are the ones in the example.

    If you want, you can download my plugin here: https://rapidshare.com/files/372773895/customPlugins.php.html

    It doesn’t do anything beside trying to add one extra option.
    And I’ve commented out what I first tried here – adding a text box for logo in General page.

    Just tested your plugin on 2.9.2, and on 3.0 beta1, works in both cases.

    I even renamed the option, to abcdef (totally random), before and after the change the option value was saved correctly.

    I simply input some random text into the field, eg. hgjgdaugyug ..

    Just tested a path to(incase that was the issue), one/two/three also saves fine.

    So i can’t replicate the problem, it’s working in both installations here just fine.

    Thread Starter Steven

    (@spstieng)

    Thanks for taking time to test this.
    At least we have clarified that the problem is not in my coding.

    I will test the same code at work and on my laptop, and see if it works there.

    Thanks a bunch!

    It’s no problem, only took all of about 5 minutes .. ??

    Thread Starter Steven

    (@spstieng)

    Aaaaaaaaah! I found the “problem”

    It wasn’t woring because I forgot to change the input name field in the HTML.

    <input type="text" name="site_logo" value="<?php echo get_option('site_logo'); ?>" />

    stupid! stupid! stupid ??

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘add_settings_field does not save data’ is closed to new replies.