A syntax error occurs when a command is not written correctly. This may include the presence of a grammatical mistake, a misspelled word or missing symbol, or an incorrect punctuation mark in your site’s code. A syntax error is an indication that something isn’t right within your site’s code. This issue can break your website, leaving you with a blank page or error message. In WordPress, this is usually a PHP error.
WordPress syntax errors aren’t frequent, but they do occur and are relatively simple to correct. If your website is down or inaccessible, it will obviously hamper the User Experience (UX). Besides hurting your traffic and conversion rates, having a blank page where your site should be can also hurt your Search Engine Optimization (SEO) rankings. WordPress syntax error messages can be concerning, especially if you aren’t familiar with website code.
Common causes of syntax rrrors on WordPress
A syntax error may occur when you’ve pasted code incorrectly. Maybe you missed a portion when you copied the code or perhaps there is an extra closing tag at the end of the script. This is a PHP open tag: <?php, and this is a closing tag: ?>. When you copy and paste a PHP code snippet, it often includes the open tag, causing a syntax error. Since you are probably pasting the snippet into existing code, you don’t need to include the open tag. You may also get a syntax error when you are editing your theme in the WordPress Customizer.
If this happens, you’ll generally know what the problem is, or at least where in the file it’s occurring. If you aren’t sure, don’t worry. You can locate the error by making a few changes to the wp-config.php file.Another reason you might see this message is because of a plugin update or installation. The extension may not be compatible with your WordPress version, or there may be other issues at play. If you were updating or installing a plugin when the error happened, that’s most likely the source of the problem.
How to fix a syntax error on WordPress
When a syntax error occurs, you can fix it by either removing or correcting the code containing the error. Either way, you’ll need access to the file where the problem is occurring. If you’re locked out of your WordPress admin dashboard, you can access this by using a File Transfer Protocol (FTP) client. Note that you’ll always want to connect using the more secure Secure File Transfer Protocol (SFTP) rather than FTP. This will ensure the transferred data is encrypted. You will need some information to connect to your website via SFTP:
- Server/Hostname
- Username
- Password
- Port
You can find this information by logging into your web hosting account. From the account management area, look for FTP Users (or FTP Accounts, depending on your web host) and select ‘Manage Users’ to view your Host, Username, and Port settings. If you don’t know your FTP password, you can reset it with the link provided. Next, open your FTP client and enter your login credentials. Once logged in, under your WordPress site’s directory, you will see the wp-admin, wp-content, and wp-includes folders, and a list of other files.
a. Enable debugging to view the syntax error location
If you were working on your website when the error occurred, you should have a good idea of where to look for the issue. However, if you’re unsure, there’s no need to guess. Debugging will show you the exact location of the error. To enable debugging, you can add the following snippet of code to your wp-config.php file:
define( 'WP_DEBUG', true );
Be sure to put the code before /* That’s all, stop editing! Happy blogging. */ toward the bottom of the file. Once you save the file and refresh your website in your browser, you should see a text string indicating the location of the error, including the file, name, and line number. Be sure to disable debugging once you’ve fixed the syntax errors, as leaving this feature on isn’t recommended for live websites. You’ve now located the syntax error. After noting the information, it’s time to get to work fixing it.
b. Fix a syntax error caused by a plugin update
If you were installing, updating, or editing a plugin file when the syntax error occurred, the simplest and fastest solution is to disable the plugin. That’s what we’ll do first. Access your website via SFTP. Once you’re connected, go to the wp-content/plugins directory, and locate the plugin folder with the error. While there, you can either disable the plugin or correct the file that contains the error – if you know what’s causing the issue.
If not, you can disable the plugin by renaming its folder in the plugins directory. If you go to your website’s URL and refresh the browser, your site should appear normal. However, if you want to continue using the plugin, you’ll need to resolve the error rather than simply disabling it. To correct the plugin error, locate the file and line number from the error message.
Identify any missing or incorrect code on that line. If you’re unsure what’s causing the error, you can paste the snippet into a code editor to help you identify it. You can always disable the plugin as a short-term fix. Then, you can reactivate it later, once the error is corrected. This may be the best approach, especially if the plugin isn’t essential to your website’s operation.
c. Fix syntax error caused by editing a theme file improperly
To fix an error that occurred while editing your theme, access your website via SFTP, and navigate to the wp-content/themes folder. Open the appropriate theme folder and locate the file with the error – usually the functions.php file. Edit the file and correct the error. Again, the syntax error code should display the line number. If the problem occurred when you pasted a code snippet into the file, delete your edits to restore the file to its stable version. If you don’t see what’s causing the problem, you can use a code editor to help identify the error. Once you correct the problem, open a browser window, and navigate to your URL to verify that your site is up and running again.
How to avoid syntax errors in the future
Using proper syntax can help you avoid errors in the future. PHP is a simple, flexible language. You can invest a little time to learn the basics. Then, when you’re pasting code or making edits to your site’s files, you’ll know how to correct errors as you work. As another option, you can keep a code editor handy to check syntax before pasting code into your website. This is a smart practice for ensuring that a code snippet is correct before adding it to files on a live site.
Another way to prevent issues is to enable debugging when making changes to your site, in order to flag errors before going live. This is the time to make sure everything is compatible with your WordPress core files and works as it should. Finally, consider deleting any unused plugins and themes. Not only can this help prevent syntax errors, but it is also a good security measure, so it’s a win-win.