How to fix syntax error in WordPress

How to Solved syntax error in WordPress

This error usually occurs when you are trying to add code snippets into WordPress and have accidentally missed something or the code has incorrect syntax. This will result into a PHP parse error and you will see a notice like:

Parse error- syntax error, unexpected $end in /public_html/site1/wp-content/themes/my-theme/functions.php on line 278

The error message would indicate the unexpected thing found in the code and the location of the script where the error occurred with line number. To fix this issue you will have to correct the syntax. Most of the time it is a missing bracket, or some unexpected character in the code

Step 1. Determining the Corrupted File

Let’s begin by recognizing the source of the problem.

If the error appears after you’ve just installed new plugins or themes, there’s a high chance that they’re the ones causing the issue.

Alternatively, it’s also possible to trace the root of the problem from the error message because it contains the full path of the corrupted file. Furthermore, it mentions the line of the faulty code as well.

Let’s take a look at the following syntax error message:

A syntax error message appears on the homepage

We can see that the error is caused by the 72nd line of the index.php file. As such, this is the file you need to modify to fix the syntax error.

If your site shows a white blank page with no error message, enable the debug function or activate the display_errors feature by accessing hPanel -> PHP Configurations -> PHP Options. Then, you’ll see the message after you reload your site.

Step 2. Fixing the File from the File Manager

Now that you’ve spotted the cause of the syntax error, it’s time to fix it:

  1. Open hPanel’s File Manager and head over to the containing folder of the file. In this case, the folder can be accessed by navigating to public_html -> wp-content -> themes -> twentyseventeen.
  2. Double-click on the corrupted file and locate the line that causes the issue. From the example above, we’re going to open the index.php file and edit the 72nd line.
    locating syntax error in index.php file
    We can see that a very important part is missing — a semicolon to end the get_footer function.

    If you are a novice in PHP, use a PHP code checker and see the most common PHP syntax mistakes. These tools will help you debug the error and fix it.

  3. Place the semicolon on the 72nd line.
    1. <?php
    2. get_footer()
    3. ;
  4.  Hit Save & Close and reload your site.

In some cases, removing the line can also fix syntax errors. For instance, there is a comment that is missing escape characters and is thus being interpreted as code. Deleting this line will fix the error.

However, be careful if you want to delete a line as it may trigger another problem. A line that’s responsible for computing a variable’s value, for example, cannot be deleted because it is used later lines

Related Articles

Leave a Reply