I’m building a WordPress plugin using the WordPress Plugin Boilerplate and in the admin area I need to use the lodash function .startCase(). The problem is that in the console I get the error “.startCase() is not a function”. Lodash lib is correctly loaded and, as it is loaded as a dependency of my main script, is loaded before of my main script.
In effect, I can use some lodash function like _.isNumber() or _.isSymbol(), but a great number of the other functions give the same error: _.isNil(), _.camelCase(), _.startCase()…
I tried to load lodash both from a local script and from a CDN but the result remains the same.
I show you here how the scripts are enqueued:
public function enqueue_scripts() {
$current_screen = get_current_screen();
if ( 'plugin_list' === $current_screen->post_type || 'instalist_page_instalist_settings' === $current_screen->id ) {
wp_enqueue_script( 'instalist_main_js', plugin_dir_url( __FILE__ ) . 'js/instalist-admin.js', array( 'jquery', 'jquery-form', $this->plugin_name . '_sweetalert-script', $this->plugin_name . '_lodash' ), $this->version, true );
wp_enqueue_script( $this->plugin_name . '_sweetalert-script', plugin_dir_url( __FILE__ ) . 'js/sweetalert2.min.js', array(), $this->version, false );
wp_enqueue_script( $this->plugin_name . '_lodash', 'https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js', array(), $this->version, false );
}
}
btw, sweetalert works fine.
In my main script, I call the function this way:
(function ($) {
"use strict";
$(document).ready(function () {
$("#plugin_slug").on("keyup", function () {
if ($(this).val().length === 0) {
$(this).sibling("#plugin_name").val("");
return;
}
if ($(this).val().length < 4) {
return;
}
$(this).siblings("#plugin_name").val(_.startCase($(this).val()));
$(this).siblings("#plugin_name").trigger("keyup");
});
});
});
I’m on a local environment with WordPress 6.5.5 and the active theme is Twenty Twenty-four. The only 2 other plugins installed are Litespeed Cache and Plugin Check. Disabling these plugins doesn’t change anything.
I can’t guess what’s going wrong here. If you need more info; I’ll be glad to give you.
Thank you in advance for any suggestion.