In the sanitize_callback
function you can look at the second argument which is the WP_Customize_Setting
instance and access the default
property there when returning due to an empty provided value:
add_action( 'customize_register', function( WP_Customize_Manager $wp_customize ) {
$wp_customize->add_setting( 'facebook_url', array(
'default' => 'https://www.facebook.com/USERNAME',
'sanitize_callback' => function( $url, $setting ) {
if ( empty( $url ) ) {
return $setting->default; // <===== Accessing default value.
}
$url = strtolower( esc_url_raw( $url ) );
$host = wp_parse_url( $url, PHP_URL_HOST );
if ( ! preg_match( '/^(www\.)?facebook\.com$/', $host ) ) {
return new WP_Error( 'invalid_url', __( 'Invalid URL or not one for Facebook', 'kb' ) );
}
return $url;
},
) );
} );