I am learning wordpress plugin development. I have dummy question - Why, the alert message is not fired, when located in js class or in index.js?
I created new file "test.php" in my plugin folder
<?php
/*
Plugin Name: Test
Description: test plugin
Version: 1.0
Author: Jarda
*/
class Test{
function __construct(){
add_action('wp_enqueue_scripts', array($this, 'startMyScripts'));
}
function startMyScripts(){
wp_enqueue_script( 'testScript', plugin_dir_url( __FILE__ ) . '/src/index.js' );
}
$test = new Test();
}
Than I created js file "index.js" in /src/
import Search from "./modules/Search"
const search = new Search()
And a class "Search" in /src/modules
class Search{
constructor(){
alert("helo")
console.log("fired")
}
}
export default Search
I have node installed, package.json file looks like this
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "wp-scripts build",
"start": "wp-scripts start",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@wordpress/scripts": "^23.5.0"
}
}
Ok, so the path must be wp_enqueue_script( 'testScript', plugin_dir_url( FILE ) . '/build/index.js' );