• Posted: 2012-01-24
  • Author: Filip Stefansson
  • Tags: Roundups

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() {
echo '<style type="text/css">
h1 a { background-image:url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_login_logo');

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()):
# Google Analytics Code #
endif;

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() {
if ( !current_user_can( 'administrator' ) ) {
wp_die('The site will be up soon.');
}
}
add_action('get_header', 'maintenace_mode');

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') ) {
add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}

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( &$links ) {
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, get_option( 'home' ) ) )
unset($links[$l]);
}
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';
add_editor_style($your_custom_stylesheet);

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.

Share this snippet

If you like this snippet, share it with friends!