7 code snippets you should use on every site

After building dozens and dozens of WordPress sites I have come up with sort of a boilerplate-functions-file I use as a starting point when developing a new theme. In this post I’ll share some of the snippets included in that boilerplate to help you boost your template production.

Change the login logo

function custom_login_logo() {<br />
echo '&lt;style type="text/css"&gt;<br />
h1 a { background-image:url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }<br />
&lt;/style&gt;';<br />
}<br />
add_action('login_head', 'custom_login_logo');<br />

Change the logo displayed on the login screen by adding this snippet to your functions.php file. This is kind of an obvious one, but non the less an important one. It’s a good way to brand your WordPress site, especially if your doing client work.

Disable Google Analytics in preview mode

if( !is_preview()):<br />
# Google Analytics Code #<br />

This snippet makes sure that all the page refreshes you do when you’re writing a post doesn’t get noticed by Google Analytics, so that your statistics doesn’t get messed up by traffic that isn’t “real”.

Enable shortcodes in widgets

add_filter('widget_text', 'do_shortcode');

Shortcodes is a great function in WordPress that let’s you create macros to use in pages and posts. However, you can’t use them in widgets by default. This snippet changes that, so you can use them in your widgets the same way you use them in your posts and pages.

Maintenance mode

function maintenace_mode() {<br />
if ( !current_user_can( 'administrator' ) ) {<br />
wp_die('The site will be up soon.');<br />
}<br />
}<br />
add_action('get_header', 'maintenace_mode');<br />

This is a handy one to have when the site is ready for launch. Put this in functions.php to close down the site for anyone who isn’t a administrator, and then just remove it when the site is ready!

Remove update notification

if ( !current_user_can('administrator') ) {<br />
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );<br />
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );<br />

For client websites it’s a good idea to hide the update notification for anyone who is not an admin, since their website could break if they accidentally update their WordPress installation.

Disable self trackbacks

function disable_self_ping( &amp;$links ) {<br />
foreach ( $links as $l =&gt; $link )<br />
if ( 0 === strpos( $link, get_option( 'home' ) ) )<br />
unset($links[$l]);<br />
}<br />
add_action( 'pre_ping', 'disable_self_ping' );

This snippet keeps your WordPress from pinging itself when you link to another page on your site, so you don’t have to remove the comment that is created when someone mentions you.

Custom Visual Editor CSS

$your_custom_stylesheet = 'css/custom-editor-style.css';<br />

You should always make the styles in the visual editor match the styles of your website, to make it easier to see how things is going to look on the site.