5

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?

2
if ( get_option( 'my_setting' ) === false ) // Nothing yet saved
    update_option( 'my_setting', 'default_stuff' );
  • 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"); – James Heald Dec 4 '16 at 19:35
  • 2
    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. – TheDeadMedic Dec 4 '16 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 Apr 28 '17 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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