How to Solve the Parse and Syntax Error in WordPress
There’s a small group of befuddling errors that elbow themselves in, front and center, on your WordPress site, forcefully ripping it down from the world wide web:
“Parse error: syntax error, unexpected…”
“Notice: Use of undefined constant…”
“Parse error: unexpected…”
All you did was add a bit of code to adjust the look of your site slightly. So what in tarnation happened??
Fortunately, these errors can be easily resolved by correcting the code you added. Read on and I’ll show you how to fix them as well as provide you with some tips to help you prevent it happening again.
What are Parse Errors?
In the context of PHP, parsing is what happens when your code is being converted from a series of characters to the final result that visitors are going to see on your site.
These errors occur when the PHP code can’t start or finish being parsed for one reason or another. It could be that the file or database is missing, corrupt or broken, your version of PHP isn’t compatible with WordPress, your file permissions aren’t set correctly, your server ran out of PHP memory or there’s a structural error in the code.
In WordPress, the latter is typically what you may come across the most and there are three main kinds:
- Syntax error – There are problems with the semicolons, or curly brackets or quotations that were used. Either their missing or the wrong ones were included.
- Unexpected error – You forgot to include a character such as an opening or closing bracket or other possible characters.
- Undefined constant error – A character is missing in an array, when referencing a variable or other possible scenarios.
Keep in mind that these three kinds of parsing errors have many different variations depending on the specific mistake in the code. Read on for more details on this and how to fix these errors.
Fixing Parse Errors
Parse errors may seem terrifying, but they’re one of the simpler errors to resolve. Somewhere in the code you just added, there’s a character missing or out of place and if you open the offending file and correct it, the error goes away.
If you just added code yourself or you installed a plugin or theme before the error occurred, that’s where you need to look to fix the issue. Removing the code, plugin or theme should resolve the issue or you can go ahead and fix it yourself.
There’s a great way to narrow down where the cause of the error lies since the full error codes give you a great hint of where to look.
Below are the basic structures of the full parse error codes mentioned above that you’re most likely going to come across.
Syntax errors have this basic structure:
“Parse error: syntax error, unexpected
characterin path/to/php-file.php on line
Undefined constant errors are structured this way:
“Notice: Use of undefined constant
constant string– assumed ‘
constant string‘ in path/to/php-file.php on line
Unexpected errors can have different variations, but generally look like the example below:
“Parse error: unexpected
characterin path/to/php-file.php on line
Keep in mind that for undefined constant errors, the named
constant string may not actually be a string since PHP often refers to unidentified tokens as constant strings.
Similarly, unexpected parse errors may list a string instead of a character.
In each of these examples,
number refers to the line number and is going to be replaced with actual numbers such as 23 or 1256, for example, though, not necessarily those exact line numbers.
The file indicated in the error message is where the issue persists and the line number gives you a hint as to where to look for the mistake. It’s either on that line or a little bit before it.
constant string names in these structure examples give you an idea of what to look for around the indicated line number. These are what needs fixing or else they’re related to what needs correcting.
Now that you know where and what to look for, you can log into your site using your preferred FTP client to locate the file and either edit it directly in your FTP client or you can download it to your computer, edit it, then re-upload it back to your site.
For details on using FTP, check out one of our other posts How to Use FTP Properly with WordPress.
If you’re not sure what specific mistakes you’re making in your structure, you can check out these posts for details on the PHP language:
- WordPress Development for Beginners: Getting Started
- WordPress Development for Beginners: Learning PHP
- WordPress Development for Beginners: Building Themes
- WordPress Development for Beginners: Widgets and Menus
- WordPress Development for Beginners: Building Plugins
- Learning PHP for WordPress Development: A Comprehensive Guide
- How to Keep Your PHP Skills Sharp for the Next Generation of WordPress
- Learning PHP, Deeply: 8 Resources for WordPress Developers
- The Ultimate Guide to the WordPress functions.php File
Avoiding these Errors
Avoiding these errors is as easy as fixing them if you’re at least a little familiar with PHP. You can check your code for syntax mistakes automatically by running it through a PHP code validator.
Here are some free validators you can use:
Here are some tips of the most common syntax mistakes to look out for right off the bat:
- Make sure there’s only one opening (
<?php) and closing tag (
- Look for opening and closing tags in the middle of a file
- If you add code, be sure it’s included before a closing tag and not after it
- Add code shouldn’t be inserted in between a function
- Check for functions that are broken up by other ones
For details on PHP syntax, check out these resources:
You can also check out and the PHP Validation Manual for more details of how to validate your syntax.
Now you know how to fix parse errors, what causes them, and how to avoid them in the future.
You also have several links to additional resources to help you with many PHP bits and bobs and if you want to learn PHP deeply from beginning to end as well as many other WordPress topics, check out our free Academy courses.
WIN a Share of $5K
Subscribe to our blog this #hostingmonth for a chance to win one of 5 prizes of $1,000 WPMU Dev credit! Learn More.