How to add code snippets to WordPress safely

By Noiz / 19th October 2026 | 4 min

Tutorials often say "add this code to your theme's functions.php". This guide shows you a safer way to add a PHP snippet: the free Code Snippets plugin, which keeps your code apart from the theme, lets you switch each snippet on and off, and has a safe mode for when a snippet breaks the site. It is written for a Noiz client on any shared plan.

Last reviewed: 19 October 2026, against the current WordPress release. This guide is written for Noiz hosting and is kept current against WordPress. It complements, and does not replace, the official WordPress documentation linked below.

Official Documentation Reference

Why Not Edit the Theme

The theme's functions.php is replaced whenever the theme updates, so your code disappears. The built-in Theme File Editor keeps no backup copy, and one typing error there can take the whole site down, editor included. A snippet plugin avoids both: the code lives in the database, survives theme updates, and can be switched off without touching a file.

Before You Start

  • Take a full backup: How to Generate a Full Backup in Plesk.
  • Know what the code does. Use snippets from the official WordPress documentation or a source you trust, and do not add code you cannot explain in a sentence. Never add code that asks for a password or sends data to another site.
  • Check the admin email address under Settings > General reaches you. If a snippet breaks the site, WordPress emails a recovery link there.
  • You can log in to Plesk, so that you can reach wp-config.php with Files if you need safe mode.

Step 1: Install Code Snippets

Go to Plugins > Add New Plugin, search for Code Snippets, then install and activate it. A Snippets menu appears.

Step 2: Add the Snippet

  1. Go to Snippets > Add New.
  2. Enter a Title that says what it does, for example "Remove unused image size".
  3. Choose the Functions (PHP) type and paste the code. Leave out an opening <?php line if the tutorial includes one.
  4. Choose where it runs: Run everywhere, Only run in administration area, Only run on site front-end or Only run once. Pick the narrowest that works.
  5. Select Save and Activate.

Step 3: Test It

Open your site in a private browser window and check the home page, a post and anything the snippet should change. Then check the dashboard still works. To undo, go to Snippets, find the snippet and select Deactivate.

If the Site Breaks: Safe Mode

  1. In Plesk, open Files and edit wp-config.php.
  2. Above the line that says to stop editing, add:
define( 'CODE_SNIPPETS_SAFE_MODE', true );
  1. Save. No snippet runs while safe mode is on, so the site comes back.
  2. Log in to WordPress, deactivate or fix the snippet, then remove the line from wp-config.php.

If you can still reach the dashboard as an administrator, adding ?snippets-safe-mode=1 to a dashboard address also switches snippets off, without editing a file. The recovery link WordPress emails to the admin address is a third way in.

Lock the Theme File Editor

Once your code lives in Code Snippets, nobody needs the built-in file editors. Turn them off by adding this line to wp-config.php with Files:

define( 'DISALLOW_FILE_EDIT', true );

For Developers: a Must-Use Plugin

If you keep code in files and version control, put it in a single PHP file in wp-content/mu-plugins. WordPress loads it automatically, and it cannot be switched off from the dashboard, which also means a mistake can only be fixed through the file. For styling changes and child themes, see How to Safely Edit Your WordPress Theme.

Troubleshooting

  • "There has been a critical error on this website": the last snippet you activated has an error. Use safe mode.
  • The snippet does nothing: check its scope; a front-end snippet does not run in the dashboard, and the reverse.
  • The code worked in the theme but not as a snippet: it may depend on the theme loading first. Ask the snippet's author, or ask us.

Let Noiz Help

WordPress Care keeps WordPress and your plugins updated with a restore point first, so a plugin update that clashes with your snippet can be rolled back. Writing or changing custom code is quoted separately.