Last active
September 13, 2021 11:02
-
-
Save gr1zix/1a6fee0062e874efc5e8a2454f344f84 to your computer and use it in GitHub Desktop.
Elementor register scripts on elementor template to overwrite the theme defaults
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('app_get_theme_version')) { | |
function app_get_theme_version() { | |
$theme_info = wp_get_theme(); | |
if ( is_child_theme() ) { | |
$theme_info = wp_get_theme($theme_info->parent_theme); | |
} | |
$theme_version = $theme_info->display('Version'); | |
return $theme_version; | |
} | |
} | |
function load_app_scripts() { | |
$template_dir = get_template_directory_uri(); | |
$theme_version = app_get_theme_version(); | |
// Register local styles file | |
wp_enqueue_style( 'custom-style', $template_dir . '/app.css', array(), $theme_version); | |
// Register CDN file | |
wp_enqueue_script('jquery-easing-js', '//cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js', array( 'jquery' ), $theme_version, true); | |
// Register Custom JS File | |
// wp_enqueue_script('script-unique-name', | |
// 'path/to/file - shoud use get_template_directory_uri() if file is local or just //cdn.com/file.js for external', | |
// 'Script requires, in this case app.js will wait until jquery will be registered and the will be loaded', | |
// 'theme ver need for cache and verstion controll', | |
// 'Second param is bool, and told system where to load the script, is true=footer, false=header') | |
wp_enqueue_script('app-js', $template_dir . '/js/app.js', array('jquery'), $theme_version, true); | |
} | |
add_action('wp_enqueue_scripts', 'load_app_scripts'); | |
// This Action sould be used for elemntor when after script will be registered on prev function don't inited on theme. | |
// It can be if you use non theme related themplates (When you creates custom homepage and selected Page template from Elementor Collection) | |
add_action( 'elementor/frontend/after_enqueue_styles', function() { | |
$template_dir = get_template_directory_uri(); | |
$theme_version = app_get_theme_version(); | |
wp_enqueue_style( 'app-custom-style', $template_dir . '/app.css', array(), $theme_version ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment