4 ways to add Custom Code in WordPress

With the WordPress themes you may start your own blog or website with minimum efforts. However, some people have more specific demands in terms of functionalities and they might decide to write their own custom code to meet the needs. It could be challenging for the users as these custom code snippets are needed to be added in the crucial functions.php file and a minute mistake in the process or coding can have fatal effects on your site. So, in this tutorial, we are going to share different ways to add custom code snippet in the WordPress site.

Add through functions.php file

Open the functions.php file of your theme and insert the code there. Though this one line step might seem to be the easiest one, it is not really recommendable due to the risk involved. Any time you will update the theme the custom code will also be cleared along with other data.

Performance updates, bug fixes, improved security and other developments make it necessary to update the theme and you may forget to copy and save your code snippet before updating the theme.

Try to avoid this method, especially, if you have purchased a paid WordPress theme, you might need to update it on a more frequent basis.

Use Child Theme

A better method of inserting custom code snippets is to create a child theme and insert it there.  The users of child theme would need to insert the code at the end of the functions.php file of their child theme.

Note: insert the code after the last line. In case your file ends with a closing PHP tag (?), then make sure that the code of the function should be inserted above that code.

However, this method also exposes you to the same disadvantage of losing your code when you change your child theme without copying and saving your custom code. So, you need to save the code in your system or jot it down. Either way it complicates the things and isn’t a reliable way.

Creating a custom plugin for storing custom snippet

This method needs more efforts and expertise but in the long run it proves to be much more reliable than the first 2 methods we discussed above as your custom code isn’t affected even when you update the theme in the future.

·         Create an empty file with a relevant name using text editor. Copy and paste it at the top.

Here’s how it should look like

<?php
/*
Plug-in Name: Your custom function name
Version:     1.0
*/

·         The custom code can be inserted below this line. Once done, save the file with a relevant name making sure that it should end with the extension “.php”

·         Go to your server and upload the file in yoursite/wp-content/plugins/. Depending upon your preference or convenience you can either upload directly through hosting panel or use Filezilla for the purpose.

If you are concerned about the size of the file then you can also convert it into a zip file to reduce the weight. After that you can upload the zipped file through the WP dashboard through plugins menu.

·         Now you need to activate the plug-in through WP Dashboard by selecting the activate option on your plugins page.

·         You can easily add or edit your code in the future as per the requirements.

This method is most reliable among the 3 options we have discussed until now. However, the major limitation is that it requires you to have an advanced WordPress knowledge and isn’t recommended for non technical users.

Use a WP plugins for adding custom functions

We understand that the above-mentioned method can be overwhelming for the new users and everyone is not prepared to go through a learning curve just to be able to customize the functionality of their WP site. Hiring a WordPress developer for the same can inflate the costs. That is why we present a beginner-friendly way to add custom code that is equally recommendable for the nontechnical as well as seasoned WordPress users. We have discussed 2 Plugins here- My custom Functions and Code Snippets Plugin

My Custom Functions

·         Just visit the WordPress website, search for My Custom functions plug-in and download it.

·         Go to your WP dashboard and open the plug-in page. Click add new and install the My Custom Functions plug-in

·         After the successful installation activate the plug-in

·         (Version 4.10 or above) Visit Settings and click on PHP inserter

·         (Versions below 4.10) Select custom functions from the appearance menu

·         Switch toggle button to On and save changes

Code Snippets Plugin

The Code Snippets is a fantastic plug-in that makes the things easier even for the users with limited technical knowledge.

·         Install and activate the plug-in

·         Observe your WordPress admin area and you would notice a new menu called snippets that is meant for storing the custom code snippets.

·         Open Add new snippet page and insert the code in the textbox named Code. Give it a relevant name under Add new snippet textbox.

·         If required you can also add a description in the text area just below the coding area.

·         After the description area you can add the relevant tags for organizing the code in a better and more comprehensible manner.

·         Now click save changes button.

·         The default option Run Code Anywhere is fine if you don’t have any specific demands. However, the users with specific demands can also choose between only run in the admin area or only run in site front end as per their requirements.

·         Your code snippet would start working immediately once you have saved and activated it.

Benefits of Code Snippets Plugin

·         The plugin comes with an advanced capability of identifying any syntax error and saving your site from malfunctioning by automatically deactivating the snippet.

·         The intuitive GUI of code snippets plug-in helps offer a neat and simple ecosystem for managing the custom code snippets. Besides you would also love the easy functionality for activating, deactivating, modifying and exporting individual/multiple code snippets without going through complex processes.

·         One of the major challenges for the users of custom code is to move it to a new server. Code snippets make things easier for you. All you have to do is to open the import page from the snippets menu and go through the appropriate import process selecting the settings and requirements that match your requirements. ..

·         There is also a provision of changing the general a specific setting to suit your preferences.