Home / Admin / Using Conditional Logic to Change Date Picker Locale
Duplicate Snippet

Embed Snippet on Your Site

Using Conditional Logic to Change Date Picker Locale

This snippet works with the WPML plugin and only apply date picker locale changes when the user selects the language from WPML

<10
Code Preview
php
<?php
/**
 * Conditionally apply the date picker locale strings, depending on what the 
 * current locale is as defined by WPML.
 *
 * @link https://wpforms.com/developers/localize-the-date-picker-strings/
 */
 
function wpf_dev_datepicker_apply_locale() {
 
    if ( defined( 'ICL_LANGUAGE_CODE' ) && 'es' == ICL_LANGUAGE_CODE ) {
    ?>
 
    <script type="text/javascript">
 
    jQuery( document ).ready( function() {
 
        jQuery( '.wpforms-datepicker-wrap' ).each( function() {
            var calendar = this._flatpickr;
 
            if ( 'object' === typeof calendar ) {
                calendar.set( 'locale', 'es' );
            }
 
        } );
 
    } );
 
    </script>
 
    <?php
    }
 
}
 
add_action( 'wpforms_wp_footer_end', 'wpf_dev_datepicker_apply_locale', 10 );

Comments

Add a Comment