-
-
Save svebal/048f89260a24eb2c8d7f37c8e5c09074 to your computer and use it in GitHub Desktop.
Check if a jQuery plugin is loaded. If not, load it using the $.getScript() function.
This file contains hidden or 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
jQuery(document).ready(function ($) { | |
'use strict'; | |
function initModal() { | |
$('.popup-youtube, .popup-vimeo, .popup-gmaps').magnificPopup({ | |
disableOn: 700, | |
type: 'iframe', | |
mainClass: 'mfp-fade', | |
removalDelay: 160, | |
preloader: false, | |
fixedContentPos: false | |
}); | |
} | |
// Check if magnificPopup is loaded | |
if (jQuery().magnificPopup) { | |
initModal(); | |
} else { | |
// Load the stylesheet | |
$('<link/>', { | |
rel: 'stylesheet', | |
href: pluginLoadedParams.styleUrl | |
}).appendTo('head'); | |
// Load the script | |
$.ajaxSetup({ | |
cache: true | |
}); | |
$.getScript(pluginLoadedParams.scriptUrl, function () { | |
initModal(); | |
}); | |
} | |
}); |
This file contains hidden or 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 | |
function pluginLoaded_scripts() { | |
// Enqueue a script with jQuery as a dependency. | |
wp_enqueue_script( 'pluginLoaded', get_template_directory_uri() . '/js/pluginLoaded.js', array( 'jquery' ), '1.0.0', true ); | |
// Define our parameters | |
$params = array( | |
'styleUrl' => get_template_directory_uri() . '/css/vendor/magnific-popup.css', | |
'scriptUrl' => get_template_directory_uri() . '/js/vendor/jquery.magnific-popup.min.js', | |
); | |
// Create the JavaScript object | |
wp_localize_script( 'pluginLoaded', 'pluginLoadedParams', $params ); | |
} | |
add_action( 'wp_enqueue_scripts', 'pluginLoaded_scripts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment