Skip to content

Code Snippets for WordPress

Code snippets are small pieces of code you can add to your WordPress website. They’re a quick way to enhance features or apply fixes without needing to install a separate plugin.

Think of them as tiny, custom improvements for your site.

Code snippets

  1. Lazy load YouTube videos.
  2. Remove comment author link.
  3. Hide tags with less than 10 posts.
  4. Disable SRCSET for images.
  5. Disable /feed/ link for post comments.
  6. Disable emojis πŸ˜‚
  7. After content CTA
  8. Google Analytics
  9. Google Adsense
  10. Limit post revisions
  11. Remove language switch from login page.

How to use Code Snippets in WordPress (video tutorial)

Watch tutorial on YouTube.

Where to add code snippets?

Code snippets can be added in 2 ways:

  1. Add code snippets to /wp-content/themes/YOUR_THEME/functions.php file.
  2. Add code snippets using specific plugin.

I prefer adding code snippets using a plugin.

Because in the plugin you can:

  • Switch any code snippet on/off.
  • Prevent code snippet from running in case of PHP error.
  • Code snippets will work even when you update or switch theme.

These are 3 main reasons why I choose using plugin for managing code snippets.

How to add code snippets?

Code Snippets WordPress plugin info card.

There are many plugins that offer using code snippets. I prefer using free version of β€œCode snippets” plugin. It is simple to use and not aggressively promoting PRO features.

Add code snippet screen inside WordPress plugin.

After installing plugin, you will see menu in WordPress admin. There you can see list of your existing snippets and add new ones.

  1. Navigate to “Snippets” page from admin sidebar in WordPress. Page will list existing snippets on your website.
  2. Click onΒ  “Add new” button at the top.
  3. On opened page add title and paste your code snippet to “Code” field.
  4. Select where that code snippet should run. In most cases run everywhere or only on front-end.
  5. You can also set execution “Priority” there. Lower priority means code will run earlier.
  6. Then provide description for code snippet. It can be instructions or more info about code.
  7. Add tags for grouping code snippets.
  8. At the bottom you click on “Save changes and activate” or just “Save changes” button.

How to activate/deactivate code snippet?

Code Snippets WordPress plugin screenshot

You can easily activate/deactivate code snippets on “Snippets” β†’ “All snippets” page with a switch button. This feature works similar to activate feature of plugins in WordPress.

If you manage multiple websites then you can export and import multiple snippets from one website to another.

Why use code snippets instead of separate plugins?

  • Plugins need to be updated to latest version of WordPress. If not updated repository will show warning message in plugin repository.
  • Plugin may add aggressive marketing links, banners when they get popular.
  • Some plugins include multiple features while you may need only single feature to solve problem.

Old (not compatible) plugin warning message in WordPress repository,

Here is the old plugin warning message. When plugin is not updated for a long time WordPress starts to show warning message.

Some security plugins also will show warning inside admin area suggesting to remove old plugins.

What is next:

 

Leave a Reply

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