Hi all,
I’m getting really confused and a bit frustrated by Sage 9, nothing I’m used to in my long experience working with WordPress seems to work any more!
I am simply trying to enqueue a new .js file - ajax.js and localise admin_ajax. I’ve followed the docs here to the letter - https://roots.io/sage/docs/theme-development-and-building/.
In setup.php I have the following:
add_action('wp_enqueue_scripts', function () {
$ajax_params = array(
'ajax_url' => admin_url('admin-ajax.php'),
'ajax_nonce' => wp_create_nonce('my_nonce'),
);
wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null);
wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true);
wp_enqueue_script('sage/ajax.js', asset_path('scripts/ajax.js'), ['jquery'], null, true);
wp_localize_script( 'sage/ajax.js', 'the_ajax_script', $ajax_params );
}, 100);
Then the contents of my ajax.js file are:
// eslint-disable-next-line no-unused-vars
function some_function(){
// eslint-disable-next-line no-undef
console.log(ajax_object);
}
Added my file to config.json:
{
"entry": {
"main": [
"./scripts/main.js",
"./styles/main.scss"
],
"customizer": [
"./scripts/customizer.js"
],
"ajax" : [
"./scripts/ajax.js"
]
The I ran yarn run buld. But when I try to call my function on a page, I’m getting ReferenceError: some_function is not defined
- what am I doing wrong? Is this a scopping issue?
Also if anyone can tell me how the heck you format code on these posts, that would also be great!!
Many thanks
Kevin