How to fix "Parse error: syntax error, unexpected" in WordPress
PHP could not read one file, almost always because code pasted into functions.php or a plugin file has a character missing. The message names the file and the line. Take the new code out, or put a clean copy of the file back, and the site returns.
- By
- WP Ministry
- Published
- Tested on
- WordPress 7.1.3, PHP 8.3.35
In short
- Nothing is lost. PHP could not read one file, so it ran none of it.
- The message names the file and a line. The mistake is on that line or shortly before it.
- Removing the code you added, or uploading the file as it was, brings the site back.
- The dashboard is down too, so the fix is made over SFTP or in your host's file manager.
- Check a file with php -l before you upload it, and try new code on a staging copy first.
A parse error means PHP, the language WordPress runs on, could not read a file. On the line the message names it met something that cannot come next in valid code, and it gave up there. PHP checks a whole file before it runs any of it, so when the file is the active theme's functions.php or part of an active plugin, every page stops, and the dashboard with them.
Nothing has been lost. Your posts, pages, settings and uploads are untouched, and the site returns as soon as the file is valid again. If you see "There has been a critical error on this website" in place of the "Parse error" line, the white screen of death page shows how to turn on the debug log, where the full line is written.
Read the message first
Parse error: syntax error, unexpected token "}", expecting ";" in /home/example/public_html/wp-content/themes/your-theme/functions.php on line 42- The path names the file. A folder under
wp-content/themes/is a theme. A folder underwp-content/plugins/is a plugin. - The line number is where PHP gave up. That is at the mistake or after it, so check that line and the few above it.
- The word after "unexpected" is what PHP met when it was expecting something else.
| The message says | The usual mistake |
|---|---|
unexpected token "}" | The statement above the brace has no semicolon. |
unexpected identifier or unexpected T_STRING | PHP met a name, such as a function's, where it expected something else. A semicolon or a closing quote is missing at the end of the line before. |
unexpected end of file | The file ended while something was still open: a last line with no semicolon, or a quote that was never closed. |
unexpected token "<", expecting end of file | A second <?php was pasted into a file that is already PHP. |
Unclosed '{' or Unmatched '}' | Braces come in pairs. One was opened and never closed, or one closes nothing. |
Here is the code behind the message above. Line 41 has no semicolon, and PHP only notices at the brace on line 42.
function example_excerpt_length() {
return 20
}
add_filter( 'excerpt_length', 'example_excerpt_length' );Pasted code goes wrong in a handful of ways:
- A second opening tag.
functions.phpalready begins with<?php. If the snippet does too, paste only what comes after it. - A missing semicolon. PHP needs one at the end of each statement.
- A brace or quote left open, because only part of a snippet was copied.
- Curly quotes. PHP writes text between straight quotes,
'or". The curly ones a web page or word processor puts in their place are not quotes to PHP. Retype them. - Code pasted after a closing
?>. PHP ignores everything outside its tags, so this code does not run at all. Paste it above the?>.
Keep it from happening again
- Put your own code in a child theme. Its
functions.phpholds only what you wrote, and an update to the parent theme does not remove it. See how to create a child theme. - Try the change on a copy first. A staging site is a private copy of your site for this.
- Check the file before you upload it, with
php -las in the first fix. - Know what the dashboard's own file editor does. When you save a PHP file there, WordPress loads the site to see whether it still works, and puts the old file back if it does not. A file uploaded over SFTP gets no such check.
- Use CSS for a change of appearance. It needs no PHP file. See how to add custom CSS.
Where it goes wrong
A page request passes through each of these in turn. This one comes from PHP, the language WordPress runs on.
- Browser
- DNS
- HTTPS
- CDN or firewall
- Web server
- PHP (this error comes from here)
- WordPress
- Database and files
What causes it
Code pasted into functions.php or a plugin file has a mistake in it
CommonA missing semicolon, a brace or quote that was never closed, a second opening tag, or curly quotes copied from a web page. One wrong character stops PHP reading the whole file.
Fix: Take out the code you added, or Put back a clean copy of the file, or Get back into the dashboard while you fix the file
An edit left a file incomplete
SometimesA character was removed while something else was being changed, and it is no longer clear what the file looked like before.
Fix: Put back a clean copy of the file, or Get back into the dashboard while you fix the file
The code needs a newer PHP than the server runs
SometimesEach PHP version adds new ways of writing code. An older PHP reads them as mistakes, so code that is correct can still stop with a syntax error.
Fix: Get back into the dashboard while you fix the file, or Run the code on the PHP version it needs
How to fix it
Take out the code you added
- Easy
- Back up first
- About 10 minutes
- Steps tested on WordPress 7.1.3
Step 1: Open the file the message names
Use SFTP or your host's file manager, and go to the line in the message.
Step 2: Remove all of what you added
Take out the whole snippet, not only the line that looks wrong. If you have a copy of the file from before the change, upload that copy instead.
Step 3: Save the file and reload the site
It loads as it did before the change. Correct the snippet, using the table above, before you add it again.
With SSH access you can do the same from the site's main folder. Print the lines around the one in the message:
sed -n '38,44p' wp-content/themes/your-theme/functions.phpKeep a copy of the file, then delete the lines you added, here lines 40 to 43:
cp wp-content/themes/your-theme/functions.php ~/functions-broken.php
sed -i '40,43d' wp-content/themes/your-theme/functions.phpThen ask PHP to check the file without running it:
php -l wp-content/themes/your-theme/functions.phpIt answers "No syntax errors detected" when PHP can read the file, and repeats the parse error with its line when it cannot.
To undo it: Upload the copy of the file you downloaded before you changed it.
Put back a clean copy of the file
- Takes care
- Back up first
- About 15 minutes
If you cannot tell what changed, replace the file with one that is known to be good.
Step 1: Get the original zip of the theme or plugin
Download it from WordPress.org, or from wherever you bought it. Choose the version you have installed if you can.
Step 2: Unzip it and find the same file
It is at the same place inside the folder as the file the message names.
Step 3: Upload it over the broken file, and reload the site
The site loads, without whatever had been added to that file.
For a plugin from WordPress.org, WP-CLI can install the whole of it again, by its slug: the last part of the address of its page on WordPress.org. This replaces every file in the plugin's folder, so a change made anywhere inside it is gone. wp theme install does the same for a theme.
wp plugin install plugin-slug --force --skip-plugins --skip-themesGet back into the dashboard while you fix the file
- Takes care
- Low risk
- About 10 minutes
- Steps tested on WordPress 7.1.3
Switching the broken theme or plugin off does not repair the file. It brings the site and the dashboard back while you work on it.
Check your email first. WordPress emails the site's administrator a link to recovery mode, which pauses the plugin or theme at fault for you alone. The white screen of death page covers that link, and how to switch a plugin or theme off by renaming its folder.
With WP-CLI, an ordinary command loads the theme and the plugins as a page does, and stops at the same parse error. Two options tell it to skip them.
Step 1: If the message names a theme, activate another one
Use the folder name of any other theme that is installed.
bashwp theme activate other-theme-folder-name --skip-plugins --skip-themesStep 2: If the message names a plugin, deactivate it
Use the plugin's folder name from the path in the message.
bashwp plugin deactivate plugin-folder-name --skip-plugins --skip-themesStep 3: Fix the file, then switch back
Use either of the first two fixes, then activate the theme or plugin again.
To undo it: Activate the theme or plugin again once its file is fixed.
Run the code on the PHP version it needs
- Takes care
- Back up first
- About 20 minutes
If the message names a line in code you never touched, and a clean copy fails the same way, the code may be correct and written for a newer PHP. The match expression and the ?-> operator, for example, were added in PHP 8.0.
Step 1: Find the version the code needs
A plugin can state the lowest PHP version it supports on a
Requires PHPline at the top of its main file. A theme can state it on the same line instyle.css.Step 2: Find the version the site runs
Look in your hosting panel, where the PHP version is set. The WordPress and PHP end-of-life checker shows whether your PHP and WordPress versions still get security fixes, and until when.
Step 3: Take a backup, then raise the PHP version
Change it in your hosting panel, or ask your host to. Everything else on the site has to work on the new version too, which is what the backup is for. If the version cannot be raised, go back to the earlier version of the plugin or theme, the one that ran.
To undo it: Set the PHP version back in your hosting panel.
When to get help
If the file passes a syntax check and the site is still down, or the line the message names is in code you never touched and a clean copy fails the same way, this is no longer a typing mistake. Finding it means reading the code against the PHP version the server runs.
Common questions
Why is the mistake not on the line the message names?
PHP reports the line where it could no longer make sense of the file, not the line where a character is missing. A missing semicolon shows up at the next line that has code on it. A quote that was never closed can show up many lines later, even at the end of the file. Read upward from the line in the message.
Can a syntax error damage my content or database?
No. A parse error is found before any of the file runs, so nothing was left half done.
The file passes the check and the site is still down. What now?
php -l checks only that PHP can read the file. It does not find errors that appear when the code runs, such as a call to a function that does not exist. The white screen of death page shows how to find those in the log. If the server answers "Internal Server Error" with no PHP message, see the 500 error page. If the fault only appears with certain plugins active together, see how to find a plugin conflict.
- GuideLocked out of WordPress admin: find which lockout you have and get back in
- GuideSlow WordPress admin: how to find the cause and fix it
- ResourceWooCommerce checkout is down: a runbook
- GuideWordPress site not showing up on Google: what to check, in order
- Guideadmin-ajax.php high CPU usage in WordPress: how to find what is calling it
- GuideHow to change WordPress permalinks without breaking your old links

