Add extra functionality to your WordPress website

WordPress is very flexible when looking to change the look and functionality of your website. You can change the theme to get a completely new look. Each theme has a set of templates that permits the customization of different areas that make up a WordPress web page. You can also add plugins to give that extra bit of functionality (that doesn’t come from WordPress core or the theme). Knowing how to use hooks and filters properly will help you modify your WordPress website so that it has the functionality you are looking for.

If you want to take a standard WordPress installation further to change how WordPress works, you must use custom code. The developers of WordPress were very clever when they designed the backend code. The coded WordPress in such a way that it is easily customizable using hooks and filters. Hooks and filters are custom coded areas inside WordPress, themes, and plugins that allow programmers to insert functionality by inserting code into the places where hooks and filters exist.

A warning is required before you start making changers to any WordPress code. When making any changes to WordPress functionality, always make a backup and download the backup (I always use the All-in-One Migration plugin for making WordPress backups). Most times, I will install a copy of the website on my testing server so I can try out code changes. This also permits testing of code changes without affecting the live site. I use a server stack from LocalWP for testing all of my websites. LocalWP is free to download and use and is available for Windows, MacOS, and Linux. Once the code changes are complete, it is a simple matter of uploading the code to the live website using All-in-One Migration.

Also, it is common practice to create a child theme and make changes to the child theme. This way, when the theme developer updates their theme, your custom coding will not be lost. Never make changes to core WordPress files, themes, or plugins. Your changes will get lost when the next update occurs.

WordPress includes templates for laying out pages, importing content, and creating sidebars. Themes and plugins also use templates for bringing in content and creating layout. Most templates can be overridden by adding the template files to the child theme folder. Using these templates and writing custom code allows you to really customize your website.

The functions.php file is a very special file in WordPress. You can add little snippets of code to the functions.php file to add extra functionality. The code snippets normally have code which attach to the hooks and filters inside WordPress. If you don’t feel comfortable editing the functions.php file in your child theme, there is a plugin called Code Snippets that allows adding custom code to the website (achieves the same effect as editing the function.php file). I use both methods on different websites. I like using Code Snippets because I have a code repository of different snippets that I can use on any website.

A filter code snippet looks like this (a filter changes how a certain area of code behaves):

function majaid_change_excerpt_length( $words ) {

 return 150;

}

add_filter( ‘excerpt_length’, ‘ majaid_change_excerpt_length’ );

This code snippet changes the default excerpt length to 150 characters. Adding this filter provides finer control over the excerpt length when displaying post excerpts on your website pages.

A hook code snippet like this (a hook inserts additional code in the area where the hook exists in the template file):

add_action( 'woocommerce_after_add_to_cart_form', 'majaid_add_shipping_time' );

function majaid_add_shipping_time() {

   $html = '<br><p>Please allow two weeks for delivery.</p>';

   echo $html;

}

This code snippet will add a custom message to the Single product page. Notice how a filter changes the default functionality of the original filter code while the hook code snippet adds a new action.

There are a ton of resources available online for finding what filters and hooks are available for WordPress. The most important resource is the WordPress Developer Resources website. Most of the better built plugins also have a list of hooks and filters. For example, WooCommerce has a list of available hooks and filters. So does Gravity Forms, a very popular forms plugin we use for most of our WordPress websites.

You can also use your search engine to find more code snippets. Business Bloomer has a lot of code snippets for WooCommerce. Adam Brown has a great collection of hooks and filters. Be careful to test any code you find on the internet before adding the code to your website. Code changes over time and some of the code may not work with the latest versions of WordPress. When copying code from websites, check the encoding as some characters will not be correct when you paste them into your editor (most common are single quotes, double quotes, and the ampersand).

Knowing how to write code to attach to WordPress and plugins is very valuable when looking to add additional functionality or change the default functionality of your WordPress website.

Need help with your WordPress website?

Let’s chat!

Share This