How To Add Custom Code In WordPress Properly?

You will find many codes on the web which can help you to accomplish your goal. For a WordPress website, you can use any PHP code to add something extra.

But do you know the proper way to add custom code in WordPress? Do you just copy and paste the code or you analyze it?

This is a serious issue because most of the people just copy and paste the code which then breaks their website. In this tutorial, you will learn the correct way to add custom code in your WordPress theme files.

What Are The Mistakes People Make While Adding The Code?

If you know a little bit about the WordPress theme then there is a file named functions.php which handles all the functions of the theme.

To increase the capability of your WordPress theme, you have to add the code in this file. People copy and paste the code in this file.

But they don’t check the basic PHP concepts. The starting tag or ending tag, they don’t check it.

MISTAKE #1.

In the functions.php file, there is a starting tag of PHP and there would be an ending tag. You should maintain that proportion of the tags.

But people add the code from any website and add it in between those tags. If the copied code already has the PHP tags then it will show an error.

For example:-

<?php 

//code of many functions

<?php

//another function

?>

?>

This is one the biggest mistakes. You haven’t closed the first tag and started a new one. The correct way to add custom code in WordPress is to close the first tag and then start another PHP tag.

<?php 

//code of many functions

?>

<?php

//another function

?>

And if you want to remove the PHP tags around another function then you can do that.

MISTAKE #2.

You add function into another function. This is the blunder. If you have a little bit of knowledge about PHP then you would know that you can’t place one function into another.

For example.

function first_one() { 

//code

function second_one() {

//code

}

}

The opening bracket for the first function is in the first line and you have added another function without closing it.

You shouldn’t do that. First, close the starting function and then start another one.

The correct way.

function first_one() { 

//code

}

function second_one() {

//code

}

MISTAKE #3.

The developers suggest that you should add the code in the bottom of the functions.php file. It doesn’t mean you add it outside the PHP tag.

There would be a closing tag for the PHP and people add the code below that.

For example.

<?php 

//code

?>

function custom_one() {

//code

}

As you can see the ending tag of PHP and you have added the function code below that. How would it work without PHP?

You have to embed the function code inside the PHP tags.

The correct way.

<?php 

//code

function custom_one() {

//code

}

?>

Though a few developers don’t like to put the end tag in the functions.php file. But you should follow the correct way to add custom code in WordPress theme files.

Though most of the WordPress users like to work with the plugins but still, there are a few things which can’t be accomplished with the plugins so you would need to find the code.

While copying and pasting the code the most important thing is to check the starting and ending tags of PHP.

Do You Know have An Idea To Add Custom Code In WordPress Theme?

Most of the time, you would need to add the code in the functions.php file. But when you work with any other theme file like single.php, index.php then you have to be very careful.

It’s because there would be many PHP and HTML tags. If you even miss a single column sign, you would break your theme.

So, it’s always good to ask an expert for an advice. If you don’t know the basics of coding then never try to play with your theme codes.

Learn the correct way to add custom code in WordPress and then try it.

by Ravi Chahar

A WordPress Professional and the LinkedIn Influencer. A coder by passion and a blogger by choice. WordPress theme development is his forte. He is your WordPress guy who will teach you how to solve WordPress errors, WordPress security issues, design issues and what not.

Get Free Updates Into Your Inbox

Learn Everything Just Like I Did

SUBSCRIBE

10 comments

  1. Hi Ravi,
    I try to stay away from code as much as possible. I have certainly broke a theme or two in the past and had to go in via ftp to figure out what I did wrong and fix it.

    It takes so much time.

    This article is very helpful and should I need to make coding changes in the future, I will be returning here BEFORE I start. ?

    1. Hey Neena,

      You should always check the PHP loop while adding any extra code. In the WordPress theme files, most of the time people make the mistake and break the whole theme.
      It’s necessary to first recognize the loop.
      Thanks for sharing your thoughts.
      Have an awesome weekend.
      ~Ravi

  2. Hello, Ravi!

    Ahh! I loved it. ?

    I happen to get hit with about the same issues often but playing with the code for awhile can make us learn to find the mistaken piece of the chunk.

    Now, it is widely clear as to how to go about adding custom code in first attempt. ?

    So, I thank you for covering this topic for us.

    See me often

    ~ Adeel

    1. Hey Adeel,

      Most of the beginners face such kind of problems. I remember I how used to struggle with the codes. And to copy and paste the PHP code in the theme files, it’s important to check the loop.
      Thanks for stopping by.
      Enjoy your day.
      ~Ravi

  3. Hello Ravi,

    You have shared more light on how to place codes in our blog’s function.php. Now a non-techie can follow your guide and execute any code without getting error.

    I never had problem adding codes to my blog because i do design blogs for a living, but there are a lot of bloggers who do not know about this stuff.

    Thanks for the brilliant post. Have a blessed weekend.

    1. Hey Babanature,

      There are many people who are struggling with the codes. For the beginners, it’s important to learn the basics of coding before copying and pasting.
      The above-mentioned are the common mistakes people make.
      Thanks for stopping by.
      ~Ravi

  4. Hi Ravi!

    Can I get in online any other easy using PHP compiler without Adobe DreamWeaver like codeblocks for C coding?
    I don’t feel comfort in DreamWeaver at the time of coding. Will you please answer me?

    Thank you
    Sadhan Pal

    1. Hey Sadhan,

      DreamWeaver is on the best tools to use. But there are some alternatives like SeaMonkey, BlueGriffon, Microsoft Expressions Web etc which are basically developed using the Mozilla platform as the main standing tool.
      But you know what, alternatives doesn’t work as good as DreamWeaver.
      ~Ravi

      1. Thank you so much @Ravil
        One more question please. Has any possibility to work properly HTML codes in a WordPress template if I want to develop this page

Leave a Reply

Your email address will not be published. Required fields are marked *