skip to main content

WordPress Functions PHP Super Useful Snippets

0

Adding Code snippets to the WordPress functions.php file enables you to perform actions and use filters in a way that will improve functionality of your WordPress site which can make your theme more organised and cleaner. Here is a bunch of useful snippets to further enhance your WordPress theme.

WordPress Website Image

Image Provided by Gil C / Shutterstock.com

Responsive Images

Having to edit, replace or upload every single image in your WordPress installation to make it responsive would be extremely daunting. This little snippet you can add a class to all your past and future images.

In this example I will use the img-responsive class from bootstrap but if you have your own replace img-responsive on line 4.

Enqueue Scripts and Styles for Internet Explorer using Conditional Comments

Thanks to WordPress version 4.2  you can now target older versions of IE using Conditional Comments without having to hardcode them in your WordPress header theme file.

The following example HTML5shiv JavaScript will load in Internet Explorer 9 and under.

This example an IE only CSS file will load in Internet Explorer 9 and under.

Redirect Author URL to About Page

If you are the only blog author then having an author page displaying all of your latest posts seems pretty pointless. Why not redirect your users to your about page instead.

Removing the version number from Scripts and Styles

Speed up your WordPress site by removing the script and styles version number which, as a result, will also improve your overall score when using PageSpeed etc.

De-Registering Styles

Interested in reducing the amount of HTTP Requests? If every plugin loads a front-end CSS file this could result in slowing your blog down further, particularly if you are not using a caching plugin.

If the plugin you use is pretty stable then you could disable the stylesheet and add the content directly to your theme stylesheet.  In this example I have disabled the contact form 7 CSS file.

Search Posts Only

If you prefer not to have pages appearing in your default WordPress search.  This little snippet will filter only the posts and include them in the search results.

Attach Permalink to Post Thumbnail

This little snippet adds a permalink to all of your post thumbnail images.

Conclusion

Do you have any favourite code snippets that you use in your WordPress blog or would like to add anymore to this list. Please feel free to send them and I’ll continue to update this post often.