Saw the “Parse error: syntax error, unexpected …” on your WordPress website frontend and freaked out? While certainly scary, as it goes to show how a tiny error, even a dot or comma, can wreck the entire website, you’re far from the first user to experience that. In fact, the error is quite common, especially among new users. If that doesn’t calm you down, know that WordPress was designed to assist you in figuring out where the problem lies. And, unlike the white screen of death issue in WordPress, it usually provides precise instructions. With that, let’s get into how to fix syntax errors in WordPress.
What causes the syntax error in WordPress?
Syntax in languages represents the way words and phrases are arranged to make a well-formed sentence. Computer language is no different – it simply refers to the structure and positioning of statements that fit the rules of the language. And, because computers don’t understand context, any character out of order, no matter how tiny, can break parts or the entire code. That’s exactly what’s happening here. Parsing e.g., the analysis of the syntax failed to identify parts of code and how they relate to each other. Remove or add missing or extra characters or even a blank space, and you’re back on track.
1. Revert the last action you made
The simplest and fastest way to solve the syntax error in WordPress is to undo whichever changes you made to your website last. Chances are, you know exactly what it is. If you can’t remember, let us refresh your mind with some typical reasons syntax error appears in WordPress:
- Copying a code snippet to “functions.php” in the wrong place or with extra characters.
- Editing some of the WordPress core files.
- Installing a new WordPress plugin or theme or updating already installed ones.
- Activating a poorly coded site-specific WordPress plugin.
- Editing your header or your footer.
- Downgrading or updating your PHP version.
2. Fix syntax error in WordPress via file path
We mentioned WordPress tries to help you. That is reflected in the error message, which typically looks something like this:
Parse error: syntax error, unexpected end of file in
/wp-content/themes/twentytwenty/functions.php on line 425
How does this help me?
The 3 bolded parts tell you what you need to know:
- end of file is the problem in this specific case. It appeared because you most likely:
- Copy-pasted the code snippet alongside an opening and/or closing PHP tag, but the file already contains both.
- Wrote opening and closing PHP tags incorrectly, for example
<? your code } ?>
instead of<?php your code ?>
- Added brackets but didn’t add a space, like this
{?> your code <?php}
instead of{ ?> your code <?php {
- Forgot to add a semicolon at the end of a function, for example:
<?php
function () {
};
- Didn’t enable short_open_tag but tried to use it. In that case, open the php.ini file in the root directory (public_html) after accessing your WordPress website via FTP. Change
short_open_tag = Off
toshort_open_tag = On
- File path refers to the folder and file where the problematic function is located. Get into your WordPress website root via FTP, and navigate to the specified folder. Open the aforementioned file via offline or web-based Text Editor.
- The number represents the line in code inside the file that needs to be fixed, depending on the problem.
What are other typical problems?
Although the exact problem varies based on your activity and WordPress files, these 3 error codes are also common:
- unexpected $end – Parsing was finished, but found that you forgot to close the symbol. This can be a comma, a quote, a bracket, a parenthesis, or something else.
- unexpected <character> (for example, unexpected “}”). The mentioned character is unnecessary on the specified line of code and should be removed.
- ‘<character>‘ expected (for example, ‘,’ expected) – Parsing was finished and the specified character should be present but isn’t.
3. I can’t fix the syntax error in WordPress via a file path. What do I do?
Although rare, the error code might exclude the file path or the line number. It can also point to far too many files and lines in code, one after another, forcing you to spend hours fixing them one by one. This commonly happens when you copied a large code snippet full of errors or installed a low-quality plugin or theme. In that case, we suggest trying these 5 ways to fix syntax errors in WordPress:
1. Restore WordPress from a backup
Are you using any of the backup plugins for WordPress? Have you created a manual backup or configured automatic backups before the problems with syntax occurred? If the answer is yes to both, restore your website from a backup. In case your backup is quite old, and syntax error mentions a limited number of files, try only replacing the files it mentions specifically.
2. Enable WP Debug
This problem is right up WP Debug’s alley. It’s a functionality built into WordPress to help developers and webmasters identify and solve problems with their code. It’s especially useful for fixing minimal typos and small bugs. We suggest trying methods 1, 2, 5, and 7 in our guide on how to debug in WordPress.
3. Use PHP code checker tools
Having trouble fixing things manually or installing WordPress plugins? Got locked out of WordPress Admin Section? In all three cases, you can employ external tools to check the PHP code on your website. We mentioned a few such tools in method 6 in the debug guide above.
4. Contact your hosting Customer Service
Most high-quality web hosting providers offer free 24/7 Customer Support via e-mail, web chat, internal ticket system, and, sometimes, phone call. And, although they’re not responsible for your mistakes, many representatives won’t mind helping if it earns them your loyalty.
5. Ask for help or hire a WordPress developer
There’s an abundance of online communities that allow posting questions and getting free, well-educated answers. Two such examples are WordPress Forums and Stack Overflow. You can also seek help in global communities with sections dedicated to it, like the WordPress subreddit on Reddit. If that fails, hiring a WordPress developer can be your last resort.