Skip to content

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

text
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 under wp-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 saysThe usual mistake
unexpected token "}"The statement above the brace has no semicolon.
unexpected identifier or unexpected T_STRINGPHP 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 fileThe 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 fileA 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.

functions.php
function example_excerpt_length() {
	return 20
}
add_filter( 'excerpt_length', 'example_excerpt_length' );
Lines 40 to 43 of the file in the message. The mistake is on line 41.

Pasted code goes wrong in a handful of ways:

  • A second opening tag. functions.php already 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.php holds 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 -l as 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.

  1. Browser
  2. DNS
  3. HTTPS
  4. CDN or firewall
  5. Web server
  6. PHP (this error comes from here)
  7. WordPress
  8. Database and files

What causes it

How to fix it

Take out the code you added

  • Easy
  • Back up first
  • About 10 minutes
  • Steps tested on WordPress 7.1.3
  1. Step 1: Open the file the message names

    Use SFTP or your host's file manager, and go to the line in the message.

  2. 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.

  3. 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:

bash
sed -n '38,44p' wp-content/themes/your-theme/functions.php

Keep a copy of the file, then delete the lines you added, here lines 40 to 43:

bash
cp wp-content/themes/your-theme/functions.php ~/functions-broken.php
sed -i '40,43d' wp-content/themes/your-theme/functions.php

Then ask PHP to check the file without running it:

bash
php -l wp-content/themes/your-theme/functions.php

It 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.

  1. 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.

  2. Step 2: Unzip it and find the same file

    It is at the same place inside the folder as the file the message names.

  3. 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.

bash
wp plugin install plugin-slug --force --skip-plugins --skip-themes

Get 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.

  1. Step 1: If the message names a theme, activate another one

    Use the folder name of any other theme that is installed.

    bash
    wp theme activate other-theme-folder-name --skip-plugins --skip-themes
  2. Step 2: If the message names a plugin, deactivate it

    Use the plugin's folder name from the path in the message.

    bash
    wp plugin deactivate plugin-folder-name --skip-plugins --skip-themes
  3. Step 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.

  1. Step 1: Find the version the code needs

    A plugin can state the lowest PHP version it supports on a Requires PHP line at the top of its main file. A theme can state it on the same line in style.css.

  2. 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.

  3. 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.

More on this subject

Would you rather we fixed it?

Emergency Fix is $99. Site down or checkout broken. Goes to the front of the queue. No fix, no fee. 30-day warranty. It starts with a free diagnosis.