WordPress Tutorial

How to fix WordPress Syntax Errors

How to fix WordPress Syntax Errors

Introduction

Every time a new WordPress user gets a Parse error: Syntax error, unexpected prompt, their hearts sink. You feel like you’ve done everything correctly, which makes the error even more discouraging. It’s also possible that you get Parse error: Syntax error, unexpected but you cannot see this error message because of WordPress error reporting being turned off. In such case, it’s strongly recommended to turn on WordPress error debug.

We are here to tell you that it’s completely normal if you’re one of these users, as WordPress can sometimes throw you off with annoying syntax errors. These errors occur if proper PHP syntax rules are not followed. In this tutorial, we will show you how to fix syntax errors on WordPress with simple instructions intended for beginners.

[optin-monster-shortcode id="cgx87nzwxn0himxw62bn"]

What you’ll need

Before starting this tutorial, you’ll need the following

  • Access to your hosting account

What is a syntax error?

At this point, you might be wondering: What is a syntax error? In computer programming, syntax errors are caused by not following the rules of specific programming languages. Think of syntax errors as analogous to placing verbs and nouns incorrectly in a sentence written in English. Just as a speaker of English won’t be able to understand the sentence, the compiler or interpreter of the programming language won’t be able to process a syntactically incorrect block of code. Whether you miss a semi-colon, or write a complete file incorrectly, you will encounter a syntax error. In the case of syntax errors appearing on WordPress, users usually make a mistake of writing PHP scripts for their sites. Syntax errors fall into the broader category of parse errors, as they are caught by parsing each line of code written for a program.

Fixing WordPress Syntax Error

Syntax errors can easily be fixed in WordPress. Essentially, it is a two-step process:

  1. Locate the line/s of code responsible for the error
  2. Modify the code by connecting to your server.

Let’s look at these steps in greater detail one-by-one.

Step 1 –  Determining Corrupted File

The first step in rectifying a syntax error is first to recognize the source of the problem. Specifically, you want to find out which file is causing the issue, and which block of code inside the file is particularly responsible for the error.

A good heuristic in such cases is reminding you of any recent plugins or themes you might have incorporated into your WordPress site. If a syntax error started appearing just after this plugin/theme was installed, you already have your answer.

Fortunately, even if you have no idea where the source of the error is, you can still easily trace its origin. When you open your site using any web browser, you are presented with the error message. The message begins with Parse error: Syntax error, followed by information specific to the error. The error message will also contain the full path of the file responsible for the error, as well as the line where the faulty code can be found. This is the file you need to modify in order to fix the syntax error.

Let’s take a look at the following syntax error message on WordPress:

Parse error: syntax error, unexpected on WordPress

From the above error message we can see that we have an unexpected end of the file. We can also see which file is corrupted (/home/u694443746/public_html/wp2/wp-content/themes/twentyseventeen/single.php) and the line of code (43) which is resulting this error. This information is more than enough for us to fix our WordPress and eliminate syntax error.

Step 2 – Fixing file with FTP Client

Now that you’ve spotted the file causing the error, you can move on to editing this file to make your WordPress site functional once again. In case you are locked out of the admin area, which happens if the code responsible for the error was injected via Appearance ->Editor area of the WordPress admin dashboard, you have to use an FTP client. For our purposes, FileZilla will suffice.

Fixing the corrupted file is straightforward: simply connect to your account with FileZilla, now, navigate over to the corrupted file that contains bad code folder, in our example it’s /home/u694443746/public_html/wp2/wp-content/themes/twentyseventeen/single.php, then rRight click on the file and choose View/Edit option.

Edit File Causing Syntax Error: Parse Error on WordPress

Corrupted file will open in your default text editor.

File causing parse error: syntax error

You now have a choice: you can either remove the faulty code or make necessary changes to ensure it works properly.

Simply removing the line can solve the issue in some cases (example: a comment that is missing escape characters and is thus being interpreted as code), but may introduce other issues not present at the time (example: a line responsible for computing a variable’s value, that is used in later lines). TTthis option is not recommended.

Now let’s investigate corrupted file more thoroughly. We already know where to search – from the error message we can see that line 43  is causing this error. Let’s see the code on this line:

It calls get_footer function and doesn’t look any different from the rest of the code. Not exactly. One small, but very important part is missing – a semicolon at the end of the line. And it’s the cause of Parse error: Syntax error error.

In our example, this error can be easily fixed by adding a semicolon at the end of the line and re-uploading the file to the server. If you are a novice in PHP, use PHP code checker to check the code and see most common PHP syntax mistakes. These tools will help you to debug the error and fix it.

Conclusion

Beginners should not lose heart when they see a Parse error: Syntax error on WordPress. It is one of the more easily identifiable, and fixable errors you will face in your journey as a developer. All you have to do is recognize which file is causing the error, narrowing it down to specific line/s, and editing that file to fix the syntax error. Of course, it is always better to avoid syntax errors in the first place, by writing code more carefully or testing it in an isolated environment before incorporating it in your website properly.

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

Share this