How to Fix the Syntax Error in WordPress

WordPress is a platform great not only for creating and managing your site but also for trying out and learning new things. And with its huge collection of tutorials, guides, and code snippets it makes it easy for you to do so.

But even with all the help and guidance in the world errors do happen and unfortunately, and even a small error can shake the functionality of your site to the core.

You are facing the #WordPress #syntaxerror face to face and have no idea what to do? This article will tell you all the steps necessary to make the error disappear! Share on X

One of those errors will be the focus of this article, and that error is the “Parse error: Syntax error, unexpected” one.

This error isn’t complicated or uncommon and you are definitely not the first nor the last person facing it. That is why when it does occur, there is no need to panic. Instead, take a deep breath and try to remember the things we are going to teach you in this article.

What is a Syntax Error in WordPress?

Before we dive into any explanations, it’s important to note that if you think your site might be facing this issue but is not notifying you in the form of an error, it could be that your WordPress reporting might be turned off.

To avoid getting yourself in that situation we recommend you always leave your WordPress error debug turned on. With that said, let’ go back to learning about the syntax error.

The word “syntax” implicates that things are supposed to occur in a certain order. Meaning if there is even a small mistake in your site’s code, for instance, a missing comma, extra curly bracket, invalid variables, wrong function names, basically anything that isn’t supposed to be there could cause the compiler to become unable to parse (process) a file and in turn, display your site.

Laptop with code

These usually small mistakes are often found in PHP scripts and can happen when a user attempts to change files or update themes/plugins.

And although the mistakes might be small, their effect is quite big considering the fact that the error they produce causes your site to become inaccessible to you and your readers. But on the upside, the message that accompanies the error, tells you exactly where you should look to resolve it. Meaning you won’t be spending days just trying to pinpoint what went wrong.

But even if the message tells you where the error is, you still have to be familiar with at least one method you can use to resolve it. And exactly that is what we will be talking about in the next part of this article.

Fixing the Error

Tools stacked on top of each other

Luckily, removing the error isn’t that big of a hassle. And can be done in two easy steps. The first step is locating the error and the second one, of course, is modifying/correcting the faulty piece of code.

Don’t worry, we are going to take everything one step at a time. So let’s get right into it!

Step 1: Find the Corrupted File

Okay, so the first thing we need to do is find the source of the problem. Meaning we have to figure out which file, and which piece of code within that file is giving us a hard time.

As we mentioned earlier, the message that notifies you of the error comes with very specific information. That information being, the full path of the file that contains the error and even the exact line of code in which the error is located.

In the image below, you can see an example of a syntax error.

syntax error

Step 2: Fix the File Using an FTP Client

Now that you know where you should be looking for the error, it’s time to go and remove it in order to make your site functional once again.

Our recommended approach for making that happen would be to use an FTP client in order to connect to the server. Once you have done that, comes the process of fixing the corrupted file which is very straightforward.

All you have to do is use the FTP client to navigate to the corrupted file, right-click on the file and choose the View/Edit option.

edit-php-file

The corrupted file will open in your default text editor.

fix wordpress syntax error

Once you have the file open, find the line of code you were warned about in the error message.

If you get to the faulty line of code but can’t tell exactly what the error within it is, you can try these tips for some guidance:

  • Have only one opening (<?php) and one closing PHP tag (?>) in a single document
  • See if there are any functions that have been broken up by other ones
  • Make sure you have a “;” at the end of each line.
  • Take a close look at your quotation marks and double-check if they are in the proper format. The proper format in PHP being, either double or single quotation marks, which are never italic.
  • Balance out your parentheses. Meaning make sure every opening parenthesis is closed.

Or, you can try using something like the PHP code checker which will browse your code for any PHP syntax mistakes.

When you do find the error, deepening on what it is, you can choose to remove the faulty code completely or change it so that it can work properly.

In some cases, the issue gets resolved by simply removing the line. But be careful with this, since it can introduce other issues as well.

After you come to a decision on what to do and have made the necessary changes to your code. Save the code file, close it, and reload your site to make sure the error is no longer present.

The error is not going away? Disable plugins and themes

If the error started after you activated/updated a theme or plugin, exactly that might be the cause of it.

In this case, probably the easiest way to solve the error would be to simply disable all your plugins and themes. Of course, you have the option of doing that manually, but who does things manually anyway?

The quicker and more convenient method would be to use something like WP Reset.

WP Reset

WP Reset is a plugin that makes debugging, testing, and resetting a piece of cake. Using it you will be able to disable all your plugins and/or themes in just a click, and they should be removed from your site in a matter of seconds.

Delete plugins option

This plugin is especially useful in these situations when you’re solving an error that has left you locked out of your admin dashboard.

Why? Well, the plugin comes with something called the Emergency Recovery Script which will enable you to delete plugins from your site, even when you no longer have access to it.

Delete plugins option in ERS

Besides that, this plugin comes with a range of other features which you can learn more about by visiting the official WP Reset website.

How To Avoid Syntax Errors?

Lastly, we want to give you a few pieces of advice regarding how you can avoid syntax errors in the first place.

Of course, the best thing would be if you took some time to learn PHP and get to know all its ins and outs. That way you will have less of a chance of causing the error when you are editing any of your site’s files, but you will also have an easier time fixing the errors if they do happen.

Also, regardless of how familiar you are with PHP, always validate your code after you have made changes to it. This can be done with tools such as the already-mentioned PHP Code Checker, W3 Markup Validation Service, PHP code Syntax Check, and etc..

Conclusion

Your heart should not stink when you face the syntax error in WordPress since it is one of the most common errors you will come across.

As you saw, it can easily be identified but also resolved. And we genuinely hope this article helped you gain a better understanding of how to do so.

Anyway, thanks for reading, and we hope to see you again on our blog!