Skip to content

Instantly share code, notes, and snippets.

@siamkreative
Created March 4, 2016 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save siamkreative/3e632c1b118440b02e54 to your computer and use it in GitHub Desktop.
Save siamkreative/3e632c1b118440b02e54 to your computer and use it in GitHub Desktop.
Check if a jQuery plugin is loaded. If not, load it using the $.getScript() function.
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();
});
}
});
<?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' );
@tripflex
Copy link

Good stuff, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment