WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] add_settings_field does not save data (17 posts)

  1. spstieng
    Member
    Posted 4 years ago #

    Hi.

    I've added a new field in my Settings->general page, following this example:http://codex.wordpress.org/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?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  3. spstieng
    Member
    Posted 4 years ago #

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

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    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..

    http://codex.wordpress.org/Creating_Options_Pages#settings_fields_Function
    http://codex.wordpress.org/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).

  5. spstieng
    Member
    Posted 4 years ago #

    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.

  6. spstieng
    Member
    Posted 4 years ago #

    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">';
    }
  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    Try..

    register_setting( 'mygroup' , 'site_logo' );

    and..

    settings_fields( 'site_logo' );

    http://codex.wordpress.org/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... ;)

  8. spstieng
    Member
    Posted 4 years ago #

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

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  10. spstieng
    Member
    Posted 4 years ago #

    Thanks dude :)
    Will compare (again) later.

    I just posted this: http://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 :)

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

    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'); ?>" />
  12. spstieng
    Member
    Posted 4 years ago #

    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: http://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.

  13. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  14. spstieng
    Member
    Posted 4 years ago #

    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!

  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    It's no problem, only took all of about 5 minutes .. ;)

  16. spstieng
    Member
    Posted 4 years ago #

    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 :)

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    Sometimes all we need is a second pair of eyes... ;)

    If it's of interest, you can see a more in-depth usage in a plugin i wrote here.
    code.google.com/p/wp-dashboard-control/source/browse/trunk/dashboard-control/dashboard-control.php

    ... which stores several settings in one option.. (simply to see as an example).

Topic Closed

This topic has been closed to new replies.

About this Topic