What WordPress snippets do you add to every project?
Have you ever thought about it before?
Here are five WordPress snippets you should considering adding when you first start a WordPress project. It’s almost a boilerplate of sorts. There may be some others I overlooked, so be sure to drop your favorite snippets off in the comments!
5 WordPress Snippets for Every Project
These five snippets can be dropped into your themes functions.php. However, if you have a set of edits you like to deploy with every project, you may want to consider building it into a functionality plugin and adding it to your plugins at the start of every project.
Maintenance Mode
[cc lang=”php”]function maintenace_mode() {
if ( !current_user_can( ‘administrator’ ) ) {
wp_die(‘The site will be up soon.’);
}
}
add_action(‘get_header’, ‘maintenace_mode’);[/cc]
This will make your WordPress site unviewable (<— not a word, apparently ;-)) for everyone except for Admins. When you’re site is ready for launch, simply comment it out or remove it completely. Personally, I would comment it out, you never know when you might want to throw it back into maintenance mode, again.
Disable Google Analytics in Preview Mode
[cc lang=”php”]if( !is_preview()):
# Google Analytics Code #
endif;[/cc]
This is a surefire way to make sure that all the page refreshes you do when you’re writing a post doesn’t get noticed by Google Analytics.
Enable Shortcodes in Widgets
[cc lang=”php”]add_filter(‘widget_text’, ‘do_shortcode’);[/cc]
Shortcodes can be really nice to sue, but they are only supported in Pages and Posts. You can, however, use this snippet to enable shortcodes in your Widget areas.
Remove Update Notification
[cc lang=”php”]if ( !current_user_can(‘administrator’) ) {
add_action( ‘init’, create_function( ‘$a’, “remove_action( ‘init’, ‘wp_version_check’ );” ), 2 );
add_filter( ‘pre_option_update_core’, create_function( ‘$a’, “return null;” ) );
}[/cc]
This is a must for client websites. This will make it so only the Admin gets these notifications. The last thing you need is a website going down because of an update deployed by a user!
Custom Visual Editor CSS
[cc lang=”php”]$your_custom_stylesheet = ‘css/custom-editor-style.css’;
add_editor_style($your_custom_stylesheet);[/cc]
It’s a good idea to have the styles in your visual editor match those of the website. With this snippet, you can do just that.
Are There Any Others?
These are only five snippets, but I’m sure there’s more.
Share’em if you got’em!
Mediastry says
Sweet! These are great…esp. the Shortcode in Widgets. Thanks…I’ll be on the lookout for others to add. This might make a good permanent section (CMS Snippets, or whatever), also.
Daniel Berman says
I second the thoughts about creating a permanent section devoted to cataloging WordPress snippets. I don’t know of any site that actually does anything like that.
Daniel Berman says
Here’s a well maintained site that archives WordPress snippets.
http://wpsnipp.com
Eric Dye says
And: https://churchm.ag/wordpress-code-snippets/
(Love wpsnipp :-))
Andrew Devlin says
Another good site is http://wp-snippets.com/
Eric Dye says
Yup!