Useful Additions for Your WordPress Functions.php File


What is Functions.php?

Functions.php is a way to add functionality and features to your WordPress website. Functions.php acts very much like a plugin but it resides in your WordPress theme folder. Its remains active as long as your theme is active. Unlike a plugin, once you change your WordPress theme the previous theme’s functions.php file becomes redundant. Utilising functions.php enables you to define your very own custom actions and filters amongst other things.

What’s the difference between functions.php and a WordPress plugin?

Functions.php is best suited for considerations relating to how your theme is displayed. Typical uses would be:

  • Adding fonts
  • Utilise featured images
  • Control excerpt length
  • Creating sidebars and widgetising your website design
  • Overriding or removing functionality from your parent theme, when using a child theme.

Plugins are better suited to scale your website’s functionality that goes beyond the look and feel of your theme. One good use of a plugin would be to register custom post types. Post types are the type of thing you would want to keep regardless of which theme you use. So, in short, if you don’t want to loose it when you switch themes, make it a plugin.

Useful custom functions

Custom excerpt length

An except length that is too long or too short can really effect the aesthetics of your website design. This great little snippet lets you take control of your unruly expert lengths.

//custom excerpt length
function wpms_custom_excerpt_length( $length ) {
//the amount of words to return return 30;
add_filter( 'excerpt_length', 'wpms_custom_excerpt_length');

Removing the WordPress version number

Hackers often look for your WordPress version to exploit a vulnerability. Why make it easy for them? The following snippet is ideal for removing the WordPress version number and making your WordPress website that bit more secure.

remove_action('wp_head', 'wp_generator')

Adding Browser or Devices Specific Body Classes

Can’t get something to look quite right in IE? Then consider binding a browser specific CSS class to your DOM and CSS target that page element in a specific browser or device with this handy function.

function browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;
if ($is_lynx) $classes[] = 'lynx';
elseif($is_gecko) $classes[] = 'gecko';
elseif($is_opera) $classes[] = 'opera';
elseif($is_NS4) $classes[] = 'ns4';
elseif($is_safari) $classes[] = 'safari';
elseif($is_chrome) $classes[] = 'chrome';
elseif($is_IE) $classes[] = 'ie';
else $classes[] = 'unknown';
if ($is_iphone) $classes[] = 'iphone';
return $classes;

Include a Paypal Donate Shortcode to Your Articles

You have gone to a lot of trouble to write your posts, why not get paid for it? With this code you can enable a shortcode to insert a Paypal donate link in to your posts.

function donate_shortcode( $atts ) {
'text' => 'Make a donation',
'account' => 'REPLACE ME',
'for' => '',
), $atts));
global $post;
if (!$for) $for = str_replace(" "," ",$post->post_title);
return ''.$text.'';
add_shortcode('donate', 'donate_shortcode');

Be sure to change your account information.

And Finally

Keep an eye on those rogue character spaces in your code. Spaces are the most common cause of PHP errors when editing your functions.php file. Also take a minute to consider if what you want to achieve would be better executed in functions.php or as a WordPress plugin.

For any help and assistance with your functions.php file or with WordPress. Please get in touch with us and find out how we can help you with any problems and issues you’re facing.

👇 Like what you read? Share what we said! 👇