7

I'm creating a simple settings page by following these instructions.

Since this is a custom plugin, I'd like one of my settings to start with a predefined value (that can later be changed). What is the recommended way to set a default value for a setting added with add_settings_field?

1 Answer 1

4
if ( get_option( 'my_setting' ) === false ) // Nothing yet saved
    update_option( 'my_setting', 'default_stuff' );
3
  • Its worth noting that if you're doing theme development (using Wordpress's theme customization API) that get_theme_mod() has a parameter for defaults. So get_theme_mod("secondary_header_text", "Hello world"); Commented Dec 4, 2016 at 19:35
  • 3
    Just to be clear, the difference is my snippet "pushes" a default option to the db, whereas get_theme_mod will return the default arg if there isn't one yet set in the db. Commented Dec 4, 2016 at 20:12
  • @TheDeadMedic, where does this code go? I have it here but it doesn't seem to populate it. add_action( 'admin_init', 'myVendorPrefix_mysettings_init' ); function myVendorPrefix_mysettings_init() { ... if ( get_option( 'my_setting' ) === false ) // Nothing yet saved update_option( 'my_setting', 'default_stuff' ); }
    – n8bar
    Commented Apr 28, 2017 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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