When I was talking about ThemeForest, I mentioned we had code on WordPress.org that made me sigh. Or cry depending on the day.
Here it is:
if (!defined('WP_CONTENT_URL')) define('WP_CONTENT_URL', get_option('siteurl').'/wp-content'); if (!defined('WP_CONTENT_DIR')) define('WP_CONTENT_DIR', ABSPATH.'wp-content'); if (!defined('WP_PLUGIN_URL')) define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins'); if (!defined('WP_PLUGIN_DIR')) define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
Why do I sigh?
It’s not needed.
You can use functions to determine those directories and while I’m sure someone’s thinking “But WP_PLUGIN_DIR
is shorter than plugins_url()
!” it’s not.
That code block above was used so that one line of code could exist.
include(WP_PLUGIN_DIR.'/PLUGINNAME/settings.php');
Those four lines, plus the include, could be replaced with this:
include( plugins_url( 'settings.php' , __FILE__ ) );
So yes, I sigh. Because with just a little work, you could see that there’s a more efficient way to make your plugin smaller.
Comments
2 responses to “Mailbag: What Code Makes You Sigh?”
Most programmers today cant decide when to follow patterns to keep things scalable and when to just cut to the chase with a one liner. Add to that the countless ones who become overnight WP experts and are not aware of how much the platform has already done for you .. This is sad but hardly surprising
@Jude: I know it’s a matter of education, and we have all done things the ‘hard’ way before sorting out the right way. It makes me sigh only because they did so many things so right and then they don’t think about the last step π