Usage
This function runs automatically, so it is not called manually. Is this incorrect?
Additional Notes
Currently this function does the following:
- Adds rel=”nofollow external” to outbound links
- Adds “no-icon” class to images within a tags
- Adds “no-icon” class to child a tags whose parent also has the class.
This function used to add classes such as “last-child”, “first-child”, “odd”, and “even” to appropriate elements, but has been removed since support for old browsers who needed them has been dropped.
Source File
Located in /assets/js/modules/helpers.js on line 56.
1 Hook
Find these filters and actions in the source code below to hook into them. Use wp.hooks.doAction() and wp.hooks.addFilter() in your JavaScript file.
Filters
"nebulaChosenOptions"Need a new filter hook? Request one here.
Actions
This function has no action hooks available. Request one?nebula.helpers = async function(){
if ( typeof window.gtag !== 'function' ){
window.gtag = function(){}; //Prevent gtag() calls from erroring if GA is off or blocked. This is supplemental to a similar check in analytics.php
}
nebula.removeTempQueryParameters(); //Remove certain trigger query strings
//Empty caches when debugging
if ( nebula.get('debug') || nebula.dom.html.hasClass('debug') ){
nebula.emptyCaches(); //Clear the caches
}
nebula.dom.html.removeClass('no-js').addClass('js');
jQuery("a[href^='http']:not([href*='" + nebula.site.domain + "'])").attr('rel', 'external noreferrer noopener'); //Add rel attributes to external links. Although search crawlers do use JavaScript, don't rely on this line to instruct them. Use standard HTML attributes whenever possible.
//Add general region classes (Note: not done in location.js because it is anonymized and global)
nebula.dom.body.addClass('locale-' + Intl.DateTimeFormat().resolvedOptions().locale.split('-').pop().toLowerCase());
nebula.dom.body.addClass('timezone-' + Intl.DateTimeFormat().resolvedOptions().timeZone.replaceAll(/[_\/]/gi, '-').toLowerCase());
nebula.browserPreferences();
//Note when remote fonts are ready (and track these performance timings)
if ( nebula?.site?.options?.remote_font_url ){
let fontName = nebula.site.options.remote_font_url.match(/family=([^&:]+)/); //While Google Fonts exposes the font family name in the endpoint URI, many other platforms do not, so this will not work with a wide range of other services.
if ( fontName ){
fontName = fontName[1].replace(/\+/g, ' ').trim();
if ( document.fonts.check('1em "' + fontName + '"') ){
window.performance.mark('(Nebula) Font Ready (' + fontName + ')'); //The font was already ready when checked
nebula.dom.body.addClass('remote-font-loaded font-ready-' + nebula.sanitizeClassName(fontName));
} else {
document.fonts.load('1em "' + fontName + '"').then(function(){
window.performance.mark('(Nebula) Font Loaded (' + fontName + ')'); //The font has now loaded
nebula.dom.body.addClass('remote-font-loaded font-ready-' + nebula.sanitizeClassName(fontName));
});
}
}
}
//Note the level of RAM available for a "lite" or "full" experience
if ( 'deviceMemory' in navigator ){ //Device Memory - Chrome 64+
let deviceMemoryLevel = ( navigator.deviceMemory < 1 )? 'lite' : 'full'; //Possible values (GB of RAM): 0.25, 0.5, 1, 2, 4, 8
nebula.dom.body.addClass('device-memory-' + deviceMemoryLevel);
}
//Skip to Content button clicks - skip to the content section
jQuery('#skip-to-content-link').on('click', function(){
nebula.focusOnElement(jQuery('#content-section'));
});
//Remove filetype icons from images within <a> tags and buttons. Note: these contribute to CLS because they are not animations
jQuery('a img').closest('a').addClass('no-icon');
jQuery('.no-icon:not(a)').find('a').addClass('no-icon');
jQuery('span.nebula-code').parent('p').css('margin-bottom', '0'); //Fix for <p> tags wrapping Nebula pre spans in the WYSIWYG
//Maintain tab navigability on hashchange (and when loaded with a hash). This also helps accessibility for things like skip to content links
if ( document.location.hash ){
nebula.focusOnElement(jQuery(document.location.hash));
}
//If the hash has been changed (activation of an in-page link)
nebula.dom.window.on('hashchange', function(){
let hash = window.location.hash.replace(/^#/, '');
if ( hash ){ //If the hash is not empty (like when clicking on an href="#" link)
nebula.focusOnElement(jQuery('#' + hash));
}
});
//Change the Bootstrap label for custom file upload inputs on upload
jQuery('input[type="file"].custom-file-input').on('change', function(){
if ( jQuery(this).parents('.custom-file').find('.custom-file-label').length ){
let fileName = jQuery(this).val().split('\\').pop(); //Get the filename without the full path
jQuery(this).parents('.custom-file').find('.custom-file-label').text(fileName);
}
});
//Add Bootstrap form control to WP search block
jQuery('.wp-block-search__input').addClass('form-control');
jQuery('.wp-block-search__button').addClass('btn btn-primary');
//Deactivate potential active states when the escape key is pressed
nebula.dom.document.on('keydown', function(e){
if ( e.key === 'Escape' ){
nebula.dom.document.trigger('esc'); //Trigger a simpler DOM event. Is this helpful?
jQuery('.modal').modal('hide'); //Close modals
}
});
//Nebula preferred default Chosen.js options
nebula.chosenOptions = wp.hooks.applyFilters('nebulaChosenOptions', {
disable_search_threshold: 5,
search_contains: true,
no_results_text: 'No results found.',
allow_single_deselect: true,
width: '100%'
});
nebula.dragDropUpload();
//Remove this once QM allows sortable Timings table
if ( jQuery('#qm-timing').length ){
nebula.qmSortableHelper(); //Temporary QM helper.
}
nebula.initCooldowns();
nebula.accessibilityHelpers();
};
Override
To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).
For non-module import functions:
nebula.helpers = function(){
//Write your own code here, leave it blank, or return false.
}
For dynamically imported module function overrides:
jQuery(window).on('load', function(){
nebula.helpers = function(){
//Write your own code here, leave it blank, or return false.
}
});
Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:
jQuery(document).on('nebula_module_loaded', function(module){
//Note that the module variable is also available to know which module specifically was imported
if ( typeof nebula.helpers === 'function' ){
nebula.helpers = function(){
//Write your own code here, leave it blank, or return false.
}
}
});