WordPress is a very famous and diverse blogging platform. Its community spans from web developers to amateur bloggers. Making changes in WordPress through PHP code (specially themes) is a common practice but bloggers with no coding experience usually don’t know how to use PHP in WordPress.

They find it hard to even copy and paste PHP code from WordPress tutorials. It is perfectly alright to make mistakes to eventually learn something useful and this article is for them.

There are times when you want to make changes in WordPress theme source files like header.php, footer.php, functions.php etc. You read a tutorial on internet, copy and paste the code in your WordPress theme PHP file and DARN! The code is not working. Worst, it can break your WordPress site.

We got a lot of queries from our readers on why the PHP code in the tutorial is not working or the the code is spitting our errors? We will try to address these and similar issues right here in this guide.

Any changes you have done to theme files could be lost forever next time the theme will get updated.

Woopoo's Caution

To keep your theme changes intact, always use a child theme. For more details, see our article how to create a child theme in WordPress

Back to our topic now. There are two common reasons why the PHP code is not working in WordPress:

  • You didn’t copy the code correctly.
  • You insert the PHP code in the wrong place.

Now let’s see five common mistakes which we can avoid while copying PHP code from WordPress tutorial websites. It will give you a good basic understanding why your PHP code is not working and how to fix it.

1. Code without PHP tags

Sometimes, a tutorial author asks you to write a code in a new PHP file and save it on your web server or themes directory. He is assuming that you already know that any PHP code should be under PHP tags.

  • example: The example code below written in a PHP file will not work. All the PHP files should have an opening and corresponding ending PHP tags <?php and ?>

opening php tags

  • Quick fix: Simply add the start and end PHP tags to the code and it will work like a charm.

php tags correct

2. Wrong placement of PHP code

Another common mistake is putting the PHP code in the wrong place. See an example below. This PHP code for functions.php will generate an error in WordPress because it has mistakenly entered in the wrong place.

  • example: Two opening PHP tags can’t be placed one after another as in the image below.

functions php code

  • Quick fix: Add a corresponding closing tag for the first opening PHP tag. Similarly delete the additional closing tag at the end.

correct code functions php

3. Code without closing PHP tag

It is also a common mistake not to copy the code properly. Let’s say while doing a tutorial to make changes in your WordPress theme, you accidentally missed to copy the closing PHP tag. Running the code will throw an error.

  • example: You can see from the image below the example PHP code started fine but mistakenly the closing tag has been missed.

closing tag php in wordpress

  • Quick fix: Always double check the code you have copied. Sometimes even WordPress tutorial authors make such mistakes.

php tags wordpress

4. A function within a function

PHP code works by calling functions. Functions hold the working code which are the building blocks of WordPress coding structure. A false function can break your WordPress site to stop working.

You can’t place a function within another function definition. A function definition starts and ends with curly brackets.

  • example: The image below shows an example of a function placed by mistake inside another function definition.

php functions wordpress

  • Quick fix: Undo the function you have copied and see where the function definition is ending with the curly closing bracket. Paste your code after the closing bracket.

There is difference between function definition and function call. A function call doesn’t need curly brackets.

Woopoo's Tip

5. A function without closing bracket

Same like PHP tag, function definition should end with a closing curly bracket. Failed to do so or forgot to add a curly bracket at the end of the function will generate error.

  • example: The function below is not correct as the function is missing the closing curly bracket.

wordpress function error

  • Quick fix: Always check the corresponding closing bracket has been added. Also make sure you are not copying the function inside another function.

wordpress correct function

All above scenarios are showing dummy examples with very less code. In reality WordPress PHP files hold hundreds of lines and a beginner can get confused very easily. Only functions.php file in Twenty Seventeen WordPress theme holds more than 500 lines of code.

We hope this article will definitely adds value to your WordPress beginner’s coding experience and you will be able to avoid common PHP mistakes in WordPress.

Do share your experiences using the comments section below and don’t forget to follow us on Twitter and Facebook

Gimme a Share pleeeaase!

Pin It on Pinterest

Share This