How to Customize WordPress Plugins

How to Customize WordPress Plugins

WordPress plugins are the perfect tools to improve the functionality of your website. Although, sometimes they can’t perform exactly what you want them to. So. it is very common for developers to customize and extend the plugins to meet their needs.

In this article, we are covering several methods of how you can do any customization to the plugin on your website. 

 

Choosing the right plugins to customize

You can not be 100% sure that you are going to modify the plugin after installation. But if you are considering extending its capabilities, choosing the right plugin is worth the research. 

What should you pay attention to:

  • The good plugin is being actively maintained by the author;
  • Has a user base, relevant to its niche;
  • Has a record of fixing bugs and active support;
  • Offers the features and functions you need.

These are not the only requirements. It will be a benefit if the plugin has a library of hooks allowing you to more easily extend the functionality. 

 

Why do you need customization?

WordPress plugins are the software that adds features to your website. Usually, plugins appeal to a wide range of platforms, most of the WordPress themes, but still, their functionality may not meet some of your specific needs. That is why you can always turn to customization and extend the capability of particular software. 

WordPress plugins are flexible and can be easily modified. Customization will save you time, while you can continue using your favorite software and don’t have to build your own solution from scratch. Use the core functionality of the third-party plugin, and expand it to meet your needs. 




Collaborate with the plugin developers

Probably the easiest way to apply some changes to the plugin output is to contact with plugin authors. 

The biggest WordPress plugin directory is wordpress.org. All of the developers share their contact information there. Some of them are open to comments, suggestions, and collaboration. 

If they are interested in collaboration, you can send them a patch with your recommended changes. If that is something they were considering to add, you can expect the changes with the next update or get the assistance prior to it.  



Use custom hooks

The next way to make customization is to use the hooks. What are those?

Hooks present one piece of code, that you can interact with and modify the other piece of code at a specific spot. 

Hooks are sort of a foundation for how plugins interact with the WordPress Core. Hooks are divided into two types: Actions and Filters. 

In order to use one of them, you need to write a Callback — a custom function. After that, register it with the WordPress hook for a specific filter or action. 

What is the difference between actions and filters? Actions add data or change how WordPress operates. Callback functions for Actions run at a point in the execution of WordPress and can perform some tasks. Actions do not return anything back to the calling hook.

Filters allow you to change the data during the execution of WordPress. Callback functions for Filters will accept a variable, modify it, and return it. They work in isolation and never affect global variables and output. 

In a nutshell, the main difference between actions and hooks is as following: 

The action takes the information it receives, does the task, and returns nothing back to the calling hook. A filter takes the information it receives, modifies it, and returns it. In other words: it filters something and passes it back to the hook for further use.

There are many hooks that you can use in WordPress. If the plugin you are using includes custom hooks, you will be able to extend it. Also, you can create your own.

Let’s have a look at the example of using custom hooks. 

We will be adding credit card Logos to the WooCommerce Cart Page.



add_action( 'woocommerce_after_cart_totals', 'credit_cards_available' );

function credit_cards_available() {

echo '<div class=”credit-cards”>

<i class="fa fa-cc-visa" aria-hidden="true"></i> <i class="fa fa-cc-mastercard" aria-hidden="true"></i>

</div>

<div class=”credit-cards-message”>We accept VISA and MasterCard</div>';

}

 

Via the action ‘woocommerce_after_cart_totals’ you can easily add content under the Cart Totals area. Use FontAwesome icons to display credit card logos. Under the logos, we can add some text to indicate which credit cards can be used. You can customize the look of the content. 

Such simple actions are great when you need to add content to a specific spot.

Some developers don’t include custom hooks in their plugins. In that case, you can either override callbacks or add custom hooks

 

Adding Custom Hooks

You can put the changes directly into the plugin, simply add the custom hooks that you need. Then you need to put the rest of your functionality in a separate plugin.

Next, you have to contact the developers and submit the patch to them, asking them to include the custom hooks that are required with the next update. If you are lucky, and they do so, you can upgrade and be sure that your plugin will smoothly interact with theirs. 

If they refuse to do it, you can still upgrade by manually patching your custom hooks into each new release.



Override Callbacks

Another way to alter the functionality of a WordPress plugin is by overriding the callbacks. This method helps to change the way the plugin interacts with a platform. While adding your custom callbacks, you are creating the functionality you want to see in the plugin. 

Plugins integrate with WordPress by registering callback functions for Core hooks. You can remove the callbacks and replace them with your own. Inside your callbacks, you should call the functions from the other plugin so you could recreate the functionality that you need. 

This option is recommended for mature developers



Changing Plugin Output

There could be situations when the best option is to change the plugin default output. 

The plugin is Not Relevant to Your Site

Sometimes, the terminology a plugin uses could not fit your website. For example, WooCommerce’s term ‘product’ may not perfectly fit the subject of your platform, so you want to change it.

You can do it through the use of the gettext filter. You will be able to change the word “Product” to any similar terms that would be appropriate.

Example: Change a Text String (Changing Product to Order) 

add_filter('gettext', 'translate_text');

add_filter('ngettext', 'translate_text');

function translate_text($translated) {

$translated = str_ireplace('Product', 'Order', $translated);

return $translated;

}

Here we are trying to change any instance of the word ‘Product’ to ‘Order’. The ‘gettext’ and ‘ngettext’ filters are used to do this operation. The $translated line contains both the text we want to replace (Product) and the text we are replacing it with (Order).

 

Changing or Adding Data in Special Situations

On the example of WooCommerce, we can try one more way of adding or changing the data for some special situations. 

For instance, we need to display some information to all products within a specific category. In order not to manually add this info to each product we will add a global change. 

How can we do this? By combining the is_product() WooCommerce function and the has_term() WordPress function, we target the products in a specific category, which allows us to add important information.

<?php 

// Add important information to products within the "Jackets" category.

add_action( 'woocommerce_single_product_summary', 'jacket_information', 10);

// Check to see if it's a WooCommerce product and in the "Jackets" category.

    if ( is_product() && has_term( 'Jackets', 'product_cat' ) ) {

        

        // Now, provide important information.

        function jacket_information() {

        echo 'Wear a jacket - it is very cold outside!';

        }

    }

?>



What Not to Do When Customizing WordPress Plugins

Changing the plugin’s functionality could be a difficult task sometimes, and mistakes can be made. Common issues include failing the backup of your website before the beginning of the process, or changing the plugin’s code while not saving the original code to another location.  

Fortunately, most of the mistakes can be avoided.

First, you should always save a plugin’s original code, in case you need to recall it. Also save copies of the changes you make to the plugin frequently, while still leaving the original code intact. Thus, if anything goes wrong, you can be sure that your work is saved. 

Additionally, it is better to use a staging environment when customizing a WordPress plugin or writing your own. This will ensure that the functionality of your live website is not compromised.

Plugins are a great tool to make your WordPress website perfect. Unfortunately, you could face some limitations. However, these limitations could be easily modified through customization. 

All the above methods could be applied even by an inexperienced developer. Try to improve your website by extending the functionality of any plugin. 

FURTHER READING