Essential Code Snippets for Your WordPress Theme’s functions.php

Any web developer who is familiar with WordPress knows about the functions.php file in your theme directory. This can include PHP code such as filters or functions to change the display of your WordPress install. But these codes are special because they can affect places even outside of your core theme files!

In this article I would like to share 20 helpful code snippets you can include in your functions.php file. All you need to do is create this file inside your theme directory and then copy over these codes inside the PHP <?php ?> brackets. Along with this collection you may also search on Google and find loads of similar resources for WordPress developers.

This small block of code will allow you to customize the total number of characters allowed in your post snippets. These are the brief descriptions which are produced automatically by WordPress for each blog article. They appear as the page description, and also in queries like the search template.

Just update the number to whatever character limit you would like to impose. My example of 65 is a fair set for brief content - Google usually recommends anywhere from 80-110 words for SEO descriptions.

This is another quick snippet you can use to replace the default more text. You will notice this on your blog homepage when you have posts which are longer than the character limit, this will display a permalink to the article. Just replace the string to whatever output you'd like.

Similar to our last bit, you may have noticed the 'Read more' link appends a hash symbol onto the end of your permalink. This is supposed to jump readers down to the paragraph where they ended right before clicking the link. Many webmasters find this annoying, and so here is a quick block of code for turning off that feature.

This is another heavy customization for mid-to-advanced level WordPress users. When editing a post or page you may notice WordPress automatically embeds content with <p> tags. Whenever you hit the Return key WordPress will format each block of text. This is a simple filter which disables the feature entirely.

By default WordPress will output a lot of meta tags and extra header details. If you don't mind keeping them in your code it's not a terrible idea. They are often great for SEO and help to give more credibility to your website. But some web developers would rather have a clean slate and get code processed much quicker.

Each of these remove_action() functions will remove a different piece from the WP head area. If you want to test out which line disables which features you should briefly comment out one line and see what appears in your source code.

I have included this feature which I originally styled for my own blog but have been using it frequently in other WordPress themes. You should add this code into your functions.php and then create some CSS styles to match. Target the class .first or p.first if you need to specify an element.

By default your featured image and post thumbnails will not show up inside your WP RSS feed. I would love to see the WordPress developers include this as a core feature during an upcoming release. But for now it's easy enough to copy this chunk of code and append the thumbnail image URL.

Once you log into the WordPress admin panel and find your profile page you will notice some extra contact fields. Users may fill out their own details for profiles such as Skype or Twitter. Using this code you may customize your own fields to include other methods of communication, or even other social media profiles.

If you would like direct access to all of the administrator's settings then definitely try out this brief snippet of code. For power users who know exactly where they need to go it's a lot easier navigating the backend. But it's also easy enough to comment out or even delete to revert back.

When you upload an image into WordPress the code will automatically include a width and height attribute onto the HTML tag. This data is often helpful when your page is still loading images, users will see a box placeholder instead of tiny 1x1 empty blocks.

But similarly there are responsive image techniques which sometimes require not specifying a width or height. It's possible to upload images and then add the HTML yourself, but for users who don't know how this is a great solution. You should be able to remove other attributes as well by listing them inside the regular expression.

This code will only display the WordPress update message to Administrators of the website. It is almost pointless for authors or editors to see this information because they do not usually have the ability to update the website. Only administrators would have this ability, and so this code snippet will keep your backend running a little more smoothly.

One of the code snippets I mentioned earlier is used to remove unwanted tags in the document header. This is similar except we're targeting meta tags which display on posts and pages. These tags are often just taking up space and do not provide much real valuable information to search crawlers. But just as before, feel free to comment out or remove lines of code for testing which meta tags should stay and which need to go.

If your website is prominently using the featured images in posts then you may consider adding this large function to your file. The two filter/action sets will add another column into the 'manage posts' and 'manage pages' table within the admin panel. This column will display a smaller thumbnail shot of the featured image for each piece of content.

Be aware that loading your pages will probably take just a couple extra seconds. Pulling images into the page is always a bit more tedious, and tack on the database query results as well. But it's a really nice piece of functionality worth testing out, if you have the time.

If you are curious about how your server handles each page load this may be worth adding into your document. This small block will add an HTML comment right at the end of each page in the wp_footer() function. You will see the total number of database queries, how long they took to perform, and how much memory was used on the server.

This small block of code is more useful when debugging than anything else. But there are often times when I'm building a template and I cannot figure out why one page is rendering different than all the others. This usually relates to a template type issue, especially with custom post types.

Just add this bit into your functions.php file for a couple of seconds, enough time to pull down your results. When you reload the page you'll notice the PHP print_r() method directly outputs the content into your HTML. Be sure to remove this after using, or just comment out the block to hide it with /* */ syntax.

This isn't a piece of the layout where many people think to look, but WordPress users love customizations. If you want to update some new links or text into your admin panel footer area, just copy over this block of code and edit the string value. Whatever is being output to the page will display in your admin footer text area.

Please note upfront that this code will only work for JPG/JPEG image types. However that being said, this is definitely worth using in your code if you ever upload jpegs. If you go to the original stack comment you can see a few example comparison images. There is clearly a huge different in quality and it's a very impressive customization.

When looking at the Pages table in the administrator panel you will notice there is a small column for the comments count. This is obviously helpful when going through posts, however pages are not formatted the same way and do not usually allow comments. Simply copy/paste this code snippet into your document and you'll notice a much cleaner table when editing page content.

This is one of my personal favorite snippets because it's so useful on any WordPress install. Instead of opening a new tab to search your question in Google it's so easy to get right into the WordPress online code docs. The search form will automatically open in a new window so you won't lose any unsaved work.

Many WordPress developers are creating shortcodes to embed into content. This makes it easier for writers to create full HTML widgets like video players and image slideshows. This snippet will create a shortcode which you may call in two ways. The first is simply [tinyurl] which will output the TinyURL version of the current page.

However you can pull a remote URL as a TinyURL by adding some optional parameters. The title="" parameter will display link text inside the <a href=""></a> tag. Otherwise your output text will be the same as the new TinyURL. Check out my comments in the source code for some more detailed examples.

Final Thoughts

I hope at least a few of these snippets may be useful for advancing web projects. The open source movement has really changed how we work and build digital products. I happen to be a huge fan of WordPress and I feel the CMS platform is only improving with age.

Definitely test out some of the codes above and see how they work on your own website. The functions.php WordPress theme file is powerful and you can perform some truly incredible updates with just a few lines of code. I would recommend keeping a backup, though, because one mistake will cause the whole theme to produce an error message. But along with my tips feel free to share your own thoughts with us in the discussion area below.

Best Plugins for a New WordPress Site [Free eBook]

Jake is a freelance writer and user experience designer. He can be found all over the web discussing HTML5, CSS3, and mobile responsive design trends. You can find more work on his portfolio website or follow his updates on Twitter @jakerocheleau.