Skip to content

How to safely add custom code snippets in WordPress

You may often find that the only way to customize a WordPress plugin or theme is with a custom code snippet. Most guides on how to implement the code snippets recommend adding them to the functions.php file of your theme. There are 3 main downsides to using this solution:

  1. The changes you make to the functions.php file get removed when updating your theme (unless you are using a child theme).
  2. If you make any mistakes in your code or copy incorrect code snippets, it can break your website.
  3. It becomes harder to manage the code snippets over time, as you are using more and more. You will likely forget what is the purpose of each one of them.

That being said, while there are multiple solutions to this problem, I like to focus on the easiest and safest one of them.

Add WordPress Code Snippets using a plugin

In my experience, the safest and easiest way to add custom code snippets to your WordPress website is by using a plugin. To do so, follow these steps:

1. Install and activate the Code Snippets plugin b from the WordPress plugin repository

This plugin can be used to add HTML, CSS, Javascript and PHP snippets, both in the back-end or the front-end of your website.

2. Edit an existing snippet (Snippets → All Snippets)

Depending on what type of snippet you are going to add, HTML, Javascript, CSS or PHP, I recommend you first edit one of the examples. This will give you a good starting point on how to format your snippet.
I’ve opened the Example HTML Shortcode, which shows us how to register a new shortcode on our website.

3. Create a new snippet (All Snippets → Add New)

In my example below, I have added a code snippet that changes the number of entries in the sitemaps built by Yoast SEO from 1000 to 500.
When you are done with creating your own snippet, click Save changes and activate.

4. Choose where you want the snippet to run

Under the code field, you can choose where you want the code snippet to run:

  • Run snippet everywhere
    This is the default option. It runs both in the back-end and the front-end of your website. If you have a code snippet that affects the WP admin bar, then you’d want to to run the snippet everywhere. The admin bar is visibile both on the front-end and back-end of your website.
  • Only run in the administration area
    The code snippet will only run in your WordPress admin dashboard. If you wanted to remove the Preview button on your posts and pages, then you would run the snippet in the administration area only.
  • Only run on site front-end
    This is useful for code snippets that impact the website for visitors only. Here’s an example where I add an extra field on a WooCommerce checkout page. This code should only run on the front-end.
  • Only run once
    The main reason why you’d want to only run once a script is when you want to run a MySQL query through PHP. Thus, you’d want to only run it once. For example, the snippet below will delete all comments marked as spam.
global $wpdb;

$wpdb->query("DELETE FROM wp_comments WHERE wp_comments.comment_approved = 'spam';");Code language: PHP (php)

5. Manage your snippets (Snippets → All Snippets)

If you ever need to temporarily turn off a snippet you have created, you can just switch the toggle as in the image below. It may not seem like much, but if your website uses dozens of snippets, this can be a life saver.

That’s it! You have now created your own snippet and found how to easily turn it on and off.

Benefits of using Code Snippets instead of writing them in the functions.php

  • It does not require any technical skills, such as editing theme files through FTP or creating a child theme.
  • The code editor will warn you whenever there are syntax errors in your snippet.
  • Code snippets will not be lost if you change your theme. Many code snippets are used to change the behavior of plugins, in which case you’d want to keep the code snippets even if you decide to change your website’s theme.
  • We can easily manage multiple snippets. The tags and search are fantastic time-savers if you have dozens of snippets.
  • We can selectively run the snippets only on the administration area (if you want to customize your dashboard), on the front-end (to change what the website visitors see), or run a script only once (for example, to unregister a custom post type).

Leave a Reply

Your email address will not be published. Required fields are marked *