[wp-trac] [WordPress Trac] #36633: Customizer Doesn't Save Defaults in The Database for a Theme

WordPress Trac noreply at wordpress.org
Fri Apr 22 17:45:09 UTC 2016


#36633: Customizer Doesn't Save Defaults in The Database for a Theme
--------------------------------+------------------------------
 Reporter:  mrahmadawais        |       Owner:
     Type:  enhancement         |      Status:  reopened
 Priority:  normal              |   Milestone:  Awaiting Review
Component:  Customize           |     Version:  trunk
 Severity:  normal              |  Resolution:
 Keywords:  dev-feedback close  |     Focuses:
--------------------------------+------------------------------

Comment (by westonruter):

 @mrahmadawais thanks for explaining. Yes, I can see how this would be
 confusing. If this were to be fixed, the approach I'd take would be to
 register any settings that lack values saved in the database as `dirty`
 up-front. This would ensure that when the user saves the Customizer state,
 all of these settings would be saved.

 But, I'm not entirely sure that there is a change that should be made.
 Take, for example, `get_theme_mod()` and `get_option()`. Both of these
 also take a `$default` argument. If a theme uses these functions and
 supplies a default value, then these would indeed be duplicated with any
 corresponding Customizer settings that get registered. But wouldn't this
 be the right thing to do? If someone activates a theme and never opens the
 Customizer, then all of these settings wouldn't be set in the DB either.

 What I think themes should do instead, then, is to perhaps have a theme
 config array that can be re-used both in the
 `get_option()`/`get_theme_mod()` calls and when registering Customizer
 settings, to be used as the `$default` in each case.

 For example:

 {{{#!php
 <?php
 $acme_theme_defaults = array(
         'phone_number' => '999-999-9999',
 );

 add_action( 'wp_footer', function() use ( $acme_theme_defaults ) {
         ?>
         <p>
                 You should give us a call! <?php echo esc_html(
 get_theme_mod( 'phone_number', $acme_theme_defaults['phone_number'] ) ) ?>
         </p>
         <?php
 } );

 add_action( 'customize_register', function( WP_Customzie_Manager
 $wp_customize ) use ( $acme_theme_defaults ) {
         $wp_customize->add_setting( 'acme_phone_number', array(
                 'type' => 'theme_mod',
                 'default' => $acme_theme_defaults['phone_number'],
         ) );

         $wp_customize->add_control( 'acme_phone_number', array(
                 'type' => 'text',
         ) );
 } );
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/36633#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list