PHP Snippet Code Errors

Whenever you add PHP code to your web site, there is always the risk that it might have a bug in it and cause your site to have a problem. If you’re lucky, the problem is limited to an ugly looking error message being displayed somewhere on one of your site’s web pages, but in some cases you can be left staring at a blank screen with a single error message on it where your web page should be.

The good news is that the PHP Snippet Library plugin helps you to avoid those situations by validating the PHP code before it is added to the library and causes problems with your site. That will eliminate the chances of a syntax error–often caused by a careless typo–bringing down your site, but there is still the chance of hitting an unexpected runtime error that could cause problems.

If that happens, you will need to correct the problem to get rid of the error message. If you can still access the administration pages of your site, that’s as easy as going to the PHP Snippet Library settings page and editing the offending snippet to correct the problem. If you’re not sure what’s wrong, you can always disable the snippet first so that nobody else will see the error when they go to your website.

But if you can no longer access your admin pages because of the error, then you will have to go outside of WordPress to edit the PHP snippet library file and correct the problem.

The PHP snippets are stored in the following file on your server:


where {WP_PLUGIN_DIR} is usually wordpress/wp-content/plugins but may be something different depending on your particular configuration of WordPress.

If you have FTP access to your server, find and download moz_php_snippets_library.php, edit it to fix the problem with the code, and then upload it again. If your fix is successful, you should have your blog back in working order in no time.

If you have some kind of shell access–either a standard Linux shell, or something like CPanel, then login to your account, locate the library file on your server, and edit it to fix the error.

Be careful not to change or delete any of the commented lines that begin with //=snip or /*=snip as you may break the format of the library file and that might prevent the plugin from being able to load the rest of your PHP snippets.

If, when you look at the bad snippet’s code, you’re not sure how to fix it, you can just comment it out or delete it from the file altogether, and then you can go back to the settings page later to sort it out.

Unable to Create/Write the PHP Snippet Library

If, when you access the PHP Snippet Library settings page, you’re greeted with an ugly long error message saying that the snippet library file cannot be created or is not writable, that means the plugin does not have write access to the library file on you web server.

Most web servers run a variety of Linux these days, and under Linux every file and every directory has an owner and a group associated with it. They also have a set of read/write/execute permissions, which can be different depending on whether the person (or process) accessing it is the owner of the file or just a member of the group associated with the file.

If you are seeing one of these errors, it usually means that the process running WordPress is not the owner of the library file (or the directory it is in), and the file/directory is read-only for the group that the WordPress process belongs to.

To correct the problem, you will have to login to your web server (via a command shell or FTP) and change the permissions of the library file or directory (the one listed in the error message). The command to do this is usually chmod and will probably be something like:

chmod 664 moz_php_snippets_library.php

If you’re using CPanel, the equivalent of the chmod command can be accessed from a menu on the screen once you reach the correct directory in the file manager.

If you can’t figure out how to fix the problem, or you don’t have access to your web server except through WordPress, you will probably have to contact your support team or web master to get them to fix the problem for you.