[wp-trac] [WordPress Trac] #41271: Customizer sanitize_callback gets called multiple times on setting change

WordPress Trac noreply at wordpress.org
Wed Nov 15 18:18:41 UTC 2017


#41271: Customizer sanitize_callback gets called multiple times on setting change
----------------------------+--------------------------
 Reporter:  kylejennings83  |       Owner:
     Type:  defect (bug)    |      Status:  new
 Priority:  normal          |   Milestone:  5.0
Component:  Customize       |     Version:  4.8
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:  performance
----------------------------+--------------------------
Description changed by westonruter:

Old description:

> I've noticed that the my Customizer setting sanitize_callback get's
> called multiple times (4).  I think I have deduced that it's not anything
> weird I'm doing and that   it's not a result of a conflicting plugin.
>
> Using VVV, I spun up a new site, deactivated all the plugins and using
> only the default Twenty Seventeen theme I added the following to
> functions.php file:
>

> {{{#!php
> <?php
> function wp_132423_customize_settings($wp_customize){
>     $wp_customize->add_setting( 'sidebar_size_setting', array(
>         'default' => 'wide',
>         'sanitize_callback' => function($val) {
>             error_log('boom'); // to test
>             },
>         )
>     );
>
>     $wp_customize->add_control( 'sidebar_size_control', array(
>             'label'   => 'Sizebar Size',
>             'section' => 'title_tagline',
>             'settings' => 'sidebar_size_setting',
>             'type' => 'select',
>             'choices' => array(
>                 'wide' => 'Wide',
>                 'narrow' => 'Narrow',
>             ),
>         )
>     );
> }
> add_action('customize_register', 'wp_132423_customize_settings');
> }}}
>

> Then I opened the Customizer and changed my "Sizebar Size" control from
> wide to narrow while tailing the error log which shows:
>
> {{{
>
> [07-Jul-2017 18:12:22 UTC] boom
> [07-Jul-2017 18:12:22 UTC] boom
> [07-Jul-2017 18:12:22 UTC] boom
> [07-Jul-2017 18:12:22 UTC] boom
> }}}
>

>
> I noticed this while trying to find a way to run a function in the
> backend whenever a particular setting was changed.  Seemed like the
> sanitize_callback was a good place to hook into and noticed this.

New description:

 I've noticed that the my Customizer setting sanitize_callback get's called
 multiple times (4).  I think I have deduced that it's not anything weird
 I'm doing and that   it's not a result of a conflicting plugin.

 Using VVV, I spun up a new site, deactivated all the plugins and using
 only the default Twenty Seventeen theme I added the following to
 functions.php file:


 {{{#!php
 <?php
 function wp_132423_customize_settings($wp_customize){
     $wp_customize->add_setting( 'sidebar_size_setting', array(
         'default' => 'wide',
         'sanitize_callback' => function($val) {
             error_log('boom'); // to test
             },
         )
     );

     $wp_customize->add_control( 'sidebar_size_control', array(
             'label'   => 'Sizebar Size',
             'section' => 'title_tagline',
             'settings' => 'sidebar_size_setting',
             'type' => 'select',
             'choices' => array(
                 'wide' => 'Wide',
                 'narrow' => 'Narrow',
             ),
         )
     );
 }
 add_action('customize_register', 'wp_132423_customize_settings');
 }}}


 Then I opened the Customizer and changed my "Sizebar Size" control from
 wide to narrow while tailing the error log which shows:

 {{{

 [07-Jul-2017 18:12:22 UTC] boom
 [07-Jul-2017 18:12:22 UTC] boom
 [07-Jul-2017 18:12:22 UTC] boom
 [07-Jul-2017 18:12:22 UTC] boom
 }}}



 I noticed this while trying to find a way to run a function in the backend
 whenever a particular setting was changed.  Seemed like the
 sanitize_callback was a good place to hook into and noticed this.

 ----

 Related: #32103, #42558.

--

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


More information about the wp-trac mailing list