[wp-trac] [WordPress Trac] #45812: WP_Customize_Setting sanitize_callback is given 2 parameters; PHP warning and validation fail
WordPress Trac
noreply at wordpress.org
Wed Jan 2 17:54:06 UTC 2019
#45812: WP_Customize_Setting sanitize_callback is given 2 parameters; PHP warning
and validation fail
--------------------------+-----------------------------
Reporter: marclarr | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Customize | Version: 5.0.2
Severity: normal | Keywords:
Focuses: |
--------------------------+-----------------------------
While working on a custom control for the Customizer I ran into the
following issue. Please see the code below regarding my setup, which is a
multi-setting control.
**The Issue**
When trying to save the setting(s) the sanitize_callback fails with the
following PHP error_log info: "PHP Warning: floatval() expects exactly 1
parameter, 2 given in .../wp-includes/class-wp-hook.php on line 286". The
parameters passed to the callback are 1) the input value and 2) the
customize manager object. The customizer also isn't saving the value due
to a validation fail with the following error notice: "Invalid value".
**System Info**
I'm running WP 5.0.2 (Multisite) locally using PHP 7.2.8 on MAMP. No
plugins are activated on the site. I'm making use of PHP namespaces and
the customize settings/controls are registered from within an abstract
class via a static method.
{{{#!php
<?php
// The below PHP is applied via:
// add_action( 'customize_register', __CLASS__ . '::_register_controls' );
$wp_customize->add_setting(
'nt-theme[site-width]',
array(
'default' => '1200',
'transport' => 'postMessage',
// This fails:
'sanitize_callback' => 'floatval',
// This works:
// 'sanitize_callback' => function( $val, $wp_customize ) {
// return floatval( $val );
// },
)
);
$wp_customize->add_setting(
'nt-theme[site-width-unit]',
array(
'default' => 'px',
'transport' => 'postMessage',
// 'sanitize_callback' => 'wp_strip_all_tags',
)
);
$wp_customize->add_control(
new CSS_Length_Customize_Control(
$wp_customize,
'nt-theme-site-width',
array(
'priority' => 10,
'section' => 'nt-theme-site-layout',
'label' => esc_html__("Site Width", 'nt-theme'),
'description' => esc_html__("The maximum width of the site
within the browser window.", 'nt-theme'),
'units' => ['px', '%'],
'settings' => [
'default' => 'nt-theme[site-width]',
'unit' => 'nt-theme[site-width-unit]',
],
'input_attrs' => [
'min' => 0,
'step' => 0.1,
],
)
)
);
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/45812>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list