I’ve scripts enqueued in capabilities.php file so scripts load solely on sure pages (I reduce most of them in code beneath)
add_action( 'template_redirect', 'plugin_is_page' );
perform plugin_is_page() {
if ( is_search() ) {
wp_enqueue_script('ads-search', get_template_directory_uri() . '/js/ads-search.js', array( 'adverts' ), $timestamp = filemtime( plugin_dir_path( __FILE__ ) . '/js/ads-search.js' ), true);
}
}
Now every of them makes use of dependency adverts.js
file witch accommodates array of objects (adverts) and capabilities to show them. In recordsdata like ads-search.js
i solely name stated capabilities on, like on this instance, search outcomes web page. I have to load newest model of every file everytime. For now it’s enqueued like this, with banners.js
file couse it masses on each single web page.
perform my_custom_theme_scripts() {
$ads_version = filemtime( get_stylesheet_directory() . '/js/adverts.js' );
$banners_version = filemtime( get_stylesheet_directory() . '/js/ads-banners.js' );
wp_enqueue_script(
'adverts',
get_stylesheet_directory_uri() . '/js/adverts.js',
array(),
$ads_version,
true
);
wp_enqueue_script(
'ads-banners',
get_stylesheet_directory_uri() . '/js/ads-banners.js',
array( 'adverts' ),
$banners_version,
true
);
}
add_action( 'wp_enqueue_scripts', 'my_custom_theme_scripts' );
So how do I exploit capabilities coded in dependency adverts.js
file throughout all my scripts like ads-banners.js
or ads-search.js
? Importing them wont work even when I load scripts as modules as a result of then I would want to incorporate model in import line and doing simply import { selectADS, sidebarADS, mobileADS } from "./adverts.js";
wont work.
All capabilities are coded in the identical manner, instance one:
export async perform selectADS(adNumber, aspect, adsArray) {
let usedNum = [];
let i = 0;
whereas (1) {
var randomNum = Math.ground(Math.random() * adsArray.size);
if (!usedNum.contains(randomNum)) {
let hyperlink = doc.createElement("a");
hyperlink.setAttribute("href", adsArray[randomNum].hyperlink);
hyperlink.setAttribute("goal", "_blank");
let advert = doc.createElement("img");
advert.setAttribute("alt", adsArray[randomNum].alt);
advert.setAttribute("src", adsArray[randomNum].src);
aspect.appendChild(hyperlink);
hyperlink.appendChild(advert);
usedNum.push(randomNum);
i++;
if (i == adNumber) break;
}
}
}
When I attempt to name selectADS
in one other file it throws perform shouldn't be outlined error