Sélectionner une page

How to Fix the Syntax Error on Your WordPress Website

How to Fix the Syntax Error on Your WordPress Website


You’ve probably heard the phrase: WordPress Syntax Error. This is the dreaded white screen with an error that says something like “Parse error: syntax error, unexpected…” This is a common error when things go wrong with WordPress. Fortunately it’s not (usually) a difficult error to fix. Let’s take a look at the most common causes and how to troubleshoot it to get your website back up and running as quickly as possible.

WordPress Syntax Error

Why the WordPress Syntax Error Happens

Typically, this happens because a snippet of code is placed within a theme file incorrectly or the code itself contains an error like a character or a tag in the wrong place or it’s missing altogether.

The Internet is filled with code snippets, plugins, themes, and tutorials about WordPress that promise to add new features that we want or that teach us about programming code for WordPress. Sometimes these tutorials and code snippets have errors. Sometimes we add our own errors through typos or through  trying to make modifications (guilty!). One simple error is enough to bring a WordPress site down. I’ve done it more than once and unless you troubleshoot the problem correctly, it can take hours to isolate the issue and fix it.

In the case pictured above, I created this error by deleting a closing parenthesis ) on line 35 in the functions.php file for the Twenty Sixteen theme. I simply went to the editor, opened the file, deleted the parenthesis, and saved it. It was that easy to bring my site down.

Fortunately, the error itself tells me where the problem is. Now all I need to do is get access to the functions.php file and fix it.

Fixing It with FTP

fz3_win_main

Image by image: Filezilla Project

It can sometimes be impossible to get back into your website once you’ve had this error. In this case, you’ll have to access it a different way. You can use an FTP app to access your website’s folders and make changes. There are lots of good FTP programs available. I’ve used FileZilla many times with great success. It’s free under the GNU license and works with many platforms. Some hosts provide FTP within cPanel. But they all work the same way by giving you access to the files and folders on your server.

Log in to your server with your FTP app, navigate to your theme’s folder, and find the file that has the error (this information is given within the error on your website). First, you’ll need to download the file.

FileZilla view edit

To find your theme file using your FTP app go to wp-content > themes > name of theme > name of file.

Select and download the file and then open it (or view and edit, depending on your app) to make the changes. When you’re done, save your changes and upload the file to your server (replacing the original). Once you’ve uploaded the file, you can refresh your website and it should be working.

If you don’t want to troubleshoot the code and the problem happened after installing a theme or plugin you can navigate to it and delete it. I’ve had to do that a few times. This can sometimes make your site behave abnormally though until you get back in and clean it up. Proceed with caution anytime you’re deleting something.

Troubleshooting the Error

First and foremost – remain calm. Your website isn’t damaged beyond repair. These errors are common enough that practically everyone who works with WordPress has seen it or will see it eventually. You’re not alone and panicking won’t help you. Follow a few methodical troubleshooting steps and your site will be back up and running in no time.

Errors are usually caused by missing commas, placing code on the wrong side of a curly bracket, or even placing the code within the wrong file. I’ve done all of these myself and it’s extremely easy to do.

In my example above, I’m opening the functions.php file and replacing the missing parenthesis. Fortunately I already know what the problem is, but what if you need to troubleshoot the code to find the error?

editing code

If you’re not sure where the problem is you can copy and paste the code into an IDE (interactive development environment) or code editor. Once I’ve fixed the problem I can save it and my site will be running again. Fortunately these tools are easy to find and use.

Debugging Tools

There are lots of good tools to help you debug code. Here is a quick look at a few online tools that are free to use. This is a great way to troubleshoot the code if you’re not sure what the error is. Some of the tools will highlight missing elements (such as a missing bracket) and others will let you step through the code to see how it works. All of them will run the code so you can see the results.

JS Bin

JS Bin

JS Bin helps you debug HTML, CSS, and JavaScript. You can run all three and see how they interact with each other to get the effect you want. Type in your code and hit the button to see how it works. There are several options for each language in a dropdown menu. This is a good choice if you want to see if the code works like you want it to.

More Information

JS Fiddle

JS Fiddle
JS Fiddle lets you run HTML, CSS, and JavaScript and you can select different types of frameworks and languages for each. It has a tool to help clean up your code. You can save your code and collaborate with others. This is a good choice if you’re working with a team.

More Information

jsdo.it

jsdo.it
jsdo.it runs JavaScript, HTML, CSS, etc., and highlights issues with your syntax. You can click the run button to see the results. It’s a good choice to see if the code works. This one is built around a social network and has the advantage of libraries and sharable code.

More Information

Cloud 9

Cloud 9
Cloud 9 is an online IDE with 150 different code languages. It has a built-in debugger that lets you set breakpoints, step over, step through, execute code, and even set it in any browser. You can use the free account or get a pro account starting at $19 per month. If you’re serious about coding, this is a tool to consider.

More Information

Debug Mode

In order to see a list of errors that your website has experienced you can place your WordPress installation in debug mode. This is done with code in your wp-config.php file. You’ll need to either add the code or activate the code if it already exists. If the code exists then switch the true/false Boolean values.

Local Installation

If your site is a local installation you can to turn on debug mode in order to troubleshoot the problem. This will display a list of your sites errors on the main page, so this is best used on a local installation rather than on a live site.

Go to your wp-config.php file and look for a line that says:

“That’s all, stop editing! Happy blogging.”

Above this line add this line of code:

define( 'WP_DEBUG', true );

If the code is already there just set it to true.

Live Installation

Use this code in the same place for a live site:

// Enable WP_DEBUG mode

define('WP_DEBUG', true);

// Enable Debug logging to the /wp-content/debug.log file

define('WP_DEBUG_LOG', true);

// Disable display of errors and warnings

define('WP_DEBUG_DISPLAY', false);

@ini_set('display_errors',0);

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)

define('SCRIPT_DEBUG', true);

The last line should only be included if you’ve made changes to your JavaScript or CSS files. If you already have this code in place just change from false to true to display the errors. The errors will now display in your error log.

Troubleshooting Using Plugins

Sometimes instead of seeing a PHP error with a number telling you which line contains the error you just get a blank screen. This can make it more difficult to figure out where the errors are coming from. For these cases you’ll need a little more help. If you to have access to the dashboard then you can troubleshoot using plugins. These are the most popular debugging plugins for WordPress.

Debug Bar

Debug Bar

Debug Bar adds a menu to your admin bar that shows debugging information. It tracks SQL queries, PHP warnings, notices, shows cache, etc.

Download

Debug This

Debug This

Debug This adds a debug menu to the admin bar that has 49 debug modes. It will debug code menus, images, widgets, sidebars, PHP, cache, cron, attachments, and lots more. It has an API where you can create your own debug modes if it doesn’t have what you need.

Download

Debug

Debug
Debug helps you find errors by editing the wp-config.php file so you don’t have to handle the debug mode code manually. It enables error reporting and will send you email notifications on runtime bugs. It will create an error log, display errors, and enable script debug save queries.

Download

Turn Off Themes and Plugins

Another way to help you troubleshoot, if you can get back into your dashboard, is to disable or delete any new themes and plugins that you’ve installed. Sometimes a problem can occur when there’s a conflict between plugins or themes. Turn them back on one at a time until you find the culprit. I like to open the site in another window so I still have access to the dashboard. Refresh the window for the site and continue until you’ve found the problem.

If you’re using a caching plugin then try clearing your site’s cache. You’ll do this manually through the plugin’s settings.

Tips for Avoiding Errors

  • Always make a backup before trying code on your website. This way if you do have an error you can get your site back to running quickly.
  • Test the code first. There are several places online where you can test code or you can try it out in a test server before placing it into your live WordPress site.
  • Be careful how you paste the code. One small error is all it takes to bring your website down and it’s easy to place the code in the wrong location.
  • For themes and plugins look at when it was updated and read the reviews and support to see if there are any recent issues.
  • Check to ensure you haven’t exceeded your bandwidth limit. If you have then your host might have blocked your site.

Final Thoughts

If you ever do come face to face with a WordPress syntax error just know that you’re not alone and it’s not that difficult to fix. By using a few simple tools and doing some methodical troubleshooting you can get your website back up and running in no time.

Article thumbnail image by Pro_Vector / Shutterstock.com



Source link