Created
May 3, 2013 13:36
-
-
Save joshfeck/5509145 to your computer and use it in GitHub Desktop.
An example of a somewhat complicated way of loading jQuery. Why not simply declare jQuery as a dependency and let WordPress manage the version of jQuery that is used?
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 | |
// start with setting some defaults | |
/** | |
* Set defaults | |
*/ | |
function squelch_taas_set_defaults() { | |
// Default versions of external scripts | |
$jquery_ver = '1.9.1'; | |
$jquery_ui_ver = '1.10.2'; | |
// Show welcme message | |
update_option( 'squelch_taas_showmessage', 2 ); | |
// Default options | |
set_default_option( 'squelch_taas_jquery_ui_theme', 'smoothness' ); | |
set_default_option( 'squelch_taas_jquery_ver', $jquery_ver ); | |
set_default_option( 'squelch_taas_jquery_ui_ver', $jquery_ui_ver ); | |
update_option( 'squelch_taas_load_jquery', true ); | |
update_option( 'squelch_taas_load_jquery_ui', true ); | |
// Has the user set their own jQuery / jQuery UI versions? | |
$update_jquery = (get_option('squelch_taas_jquery_ver') == get_option('squelch_taas_jquery_ver_default') ); | |
$update_jquery_ui = (get_option('squelch_taas_jquery_ui_ver') == get_option('squelch_taas_jquery_ui_ver_default') ); | |
if ($update_jquery) error_log( "Going to update jquery ver"); | |
else error_log( 'jquery ver: '.get_option( 'squelch_taas_jquery_ver') ); | |
if ($update_jquery_ui) error_log( "Going to update jquery ui ver"); | |
else error_log( 'jquery UI ver: '.get_option( 'squelch_taas_jquery_ui_ver') ); | |
update_option( 'squelch_taas_jquery_ver_default', $jquery_ver ); | |
update_option( 'squelch_taas_jquery_ui_ver_default', $jquery_ui_ver ); | |
// On upgrade, bump the version of jquery loaded UNLESS the user has overridden the versions in the config, in which case warn them | |
if ($update_jquery) update_option( 'squelch_taas_jquery_ver', $jquery_ver ); | |
else update_option( 'squelch_taas_upgrade_warning', 1 ); | |
if ($update_jquery_ui) update_option( 'squelch_taas_jquery_ui_ver', $jquery_ui_ver ); | |
else update_option( 'squelch_taas_upgrade_warning', 1 ); | |
} | |
// [...] | |
// set up some detection and warning messages if someone changes the default settings to load a different version of jQuery | |
if ( get_option( 'squelch_taas_upgrade_warning' ) > 0 ) { | |
$url = squelch_taas_get_plugin_admin_url(); | |
$url_rmv = $url.'&taas-rmvmsg=upgrade_warning'; | |
$update_jquery = !(get_option('squelch_taas_jquery_ver') == get_option( 'squelch_taas_jquery_ver_default' )); | |
$update_jquery_ui = !(get_option('squelch_taas_jquery_ui_ver') == get_option( 'squelch_taas_jquery_ui_ver_default' )); | |
if ($update_jquery || $update_jquery_ui) { | |
$jquery_ver = get_option( 'squelch_taas_jquery_ver' ); | |
$jquery_ui_ver = get_option( 'squelch_taas_jquery_ui_ver' ); | |
$jquery_ver_default = get_option( 'squelch_taas_jquery_ver_default' ); | |
$jquery_ui_ver_default = get_option( 'squelch_taas_jquery_ui_ver_default' ); | |
$msg = '<div class="error">' | |
.'<p>' | |
.'You have configured Squelch Tabs and Accordions Shortcodes to load '; | |
if ($update_jquery && $update_jquery_ui) $msg .= 'version '.$jquery_ver.' of jQuery and version '.$jquery_ui_ver.' of jQuery UI '; | |
else if (!$update_jquery && $update_jquery_ui) $msg .= 'version '.$jquery_ui_ver.' of jQuery UI '; | |
else $msg .= 'version '.$jquery_ver.' of jQuery '; | |
$msg .= '' | |
.'on your website, but the recommended '; | |
if ($update_jquery && $update_jquery_ui) $msg .= 'versions are now '.$jquery_ver_default.' and '.$jquery_ui_ver_default.'. '; | |
else if (!$update_jquery && $update_jquery_ui) $msg .= 'version is now '.$jquery_ui_ver_default.'. '; | |
else $msg .= 'version is now '.$jquery_ver_default.'. '; | |
$msg .= '' | |
.'It is highly recommended that you update your settings to the new versions on the ' | |
.'<a href="'.$url.'">settings</a> page. ' | |
.'<a href="'.$url_rmv.'">Remove this message</a></p></div>'; | |
echo $msg; | |
} | |
} | |
// [...] | |
// deregister the WP bundled copy of jQuery and "hard-wire the plugin to specific versions to prevent incompatibility problems" | |
/** | |
* Enqueue the JavaScript and CSS. | |
*/ | |
function squelch_taas_enqueue_scripts() { | |
global $taas_plugin_ver; | |
$load_jquery = get_option('squelch_taas_load_jquery'); | |
$jquery_ver = get_option('squelch_taas_jquery_ver'); | |
$load_jquery_ui = get_option('squelch_taas_load_jquery_ui'); | |
$jquery_ui_ver = get_option('squelch_taas_jquery_ui_ver'); | |
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https:" : "http:"; | |
// Piggyback the Google APIs CDN but hard-wire the plugin to specific versions to prevent incompatibility problems | |
if ($load_jquery) { | |
wp_deregister_script( 'jquery' ); | |
wp_register_script( 'jquery', $protocol.'//ajax.googleapis.com/ajax/libs/jquery/'.$jquery_ver.'/jquery.min.js' ); | |
wp_enqueue_script( 'jquery' ); | |
} | |
if ($load_jquery_ui) { | |
wp_deregister_script( 'jquery-ui-core' ); | |
wp_register_script( 'jquery-ui-core', $protocol.'//ajax.googleapis.com/ajax/libs/jqueryui/'.$jquery_ui_ver.'/jquery-ui.min.js' ); | |
wp_enqueue_script( 'jquery-ui-core' ); | |
} | |
// Enqueue the JavaScript | |
wp_enqueue_script( | |
'squelch_taas', | |
plugins_url( 'js/squelch-tabs-and-accordions.js', __FILE__ ), | |
array( 'jquery', 'jquery-ui-core' ), | |
$taas_plugin_ver, | |
true | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment