Remove type attribute from script added by wp_localize_script

How can I remove type attributes from all scripts added by wp_localize_script ?

Have the same problem with W3C validation as described here – Remove type attribute from script and style tags added by WordPress

But solution described there cannot help with wp_localize_script script’s.

As a substitute I’ve used another function wp_add_inline_script and solution from question below and helper function to prepare variables.


function prepare_var_to_localize_script(string $variable_name, $variable) {
    $var_json_string = json_encode($variable);
    return "var $variable_name = JSON.parse('$var_json_string');";
}

$data = prepare_var_to_localize_script('ajax_obj', array(
   'ajax_url' => admin_url('admin-ajax.php'),
   'ajax_wp_nonce' => wp_create_nonce()
));

wp_add_inline_script('my-script-id', $data, 'before');

0

Leave a Comment