When it comes to your WordPress site, updates are one of the single most important aspects of the site to ensure it continues to operate smoothly and that it is not vulnerable to security flaws. This is a small code snippet that completely disables all update checks:
Continue reading
Severely Over Complicated jQuery Load Process
I have seen a lot of ways to load custom versions of jQuery, but this is probably the most “robust”, though I don’t mean that in a good way.
WordPress makes it really easy to load jQuery by simply doing:
wp_enqueue_script( 'jquery' )
Some developers believe it is better to force WordPress to use other versions of jQuery from google (or elsewhere). This is a practice I strongly disagree with. You can read about why I feel this practice is really irresponsible on Pippin’s Plugins.
Anyhow this particular developer went really far and decided to build an overly complex system for loading a custom version of jQuery, and all for a really simple tabs / accordion plugin. Here it is:
<?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 | |
); |
Now, I really do not like talking badly of others, so please note that I am NOT criticizing this developer personally. It is very likely that this was developed due to his or her own experiences and frustrations with the all-to-common jQuery conflict. I’m posting this not as a insult to whoever wrote this, but as an example of why theme and plugin developers should almost never load their own jQuery. The practice of loading jQuery from Google has resulted in developers needing to find elaborate solutions like this, when really it’s way simpler: if everyone did it correctly by just allowing WordPress to handle the version of jQuery loaded, we’d never have these problems.
Submitted by Josh Feck.
Loading jQuery Poorly and Not Reading The Code Comments
I had to chuckle a bit when Eric Mann sent this snippet to me. I’ll let his words do the explaining for you:
Continue reading
Grab All the Post Meta
This snippet is a perfect case of a developer not considering what plugins might be doing with post meta. The snippet grabs all of the post meta attached to the current post and displays it, all of it. The only time meta isn’t displayed is if the meta key starts with an underscore (_), which means it is a hidden field, or if the key is “enclosure”.
Continue reading
Update All the Meta Things
This one had me pulling my hair our for a while. I found it while helping a user of one of my plugins out with a support request. The theme author included some custom post types and added some custom meta fields to those post types, all of which is perfectly fine. The problem, however, comes in with how the theme developer decided to process the saving of the meta fields.
Continue reading
Get Custom Field Fail
This is a bit of a head scratcher. It’s one of those that makes you wonder what the original developer was thinking, and not because it does something really poorly but because it does something that is completely pointless.
Continue reading
Custom jQuery
Loading custom jQuery is the single cause for a huge number of the support tickets I handle. Sometimes developers decide to load jQuery from Google, sometimes they include a version with their plugin files, sometimes they enqueue it semi-properly, and sometimes they don’t even enqueue it. This snippet is an example of all the bad ways to load jQuery in a plugin.
Continue reading
Custom Templating System
This is not entirely bad code, rather more an example of doing something that is really unnecessary. The developer of this snippet wanted a way to create custom template files based on the post type that was displayed.
Continue reading
Foreach All the Super Globals
This snippet is pretty great. It serves absolutely no purpose whatsoever and it simply silly.
Continue reading
Global Data
I’ve seen this quite a few times, and it drives me nuts every time.
Continue reading