How do you pass a boolean value to wp_localize_script [duplicate]

Unfortunately wp_localize_script() casts all scalars (simple types) in the passed-in array to strings (and then runs html_entity_decode() on them?!), so the casts mentioned by the answer you quote & @TheDeadMedic will get stringified “1”https://wordpress.stackexchange.com/”” if boolean, and number strings if ints, which won’t work with javascript plugins that demand exact values.

A way around it is to put your arguments in an array within the passed-in array, then they don’t get mangled, eg:

$data = array (
    'ng_slicknav' => array(
        'menu'         => $options['ng_slicknav_menu'],
        'parent_links' => (bool) $options['ng_slicknav_parent_links'],
        'speed'        => (int) $options['ng_slicknav_speed'] ,
    ),
);

And you can reference them directly in your javascript:

jQuery(document).ready(function($) {
    $(phpVars.ng_slicknav.menu).slicknav({
        allowParentLinks: phpVars.ng_slicknav.parent_links,
        duration:phpVars.ng_slicknav.speed,
    });
});

Leave a Comment