Tutorial

How to Properly Add PHP Code Snippets in WordPress

Most website owners use the wrong way to add PHP code snippets to WordPress.

Background

Sometimes we need to add PHP code snippets to WordPress for customization purposes. Most tutorials instruct to add the code to the file functions.php of the current theme because it is the easiest way.

Placing the code in functions.php is the easiest but not the best way. The problem is that if you update or change the current theme, all changes will be lost.

Plugin

Use the plugin Code Snippets to properly add custom PHP code to WordPress. This plugin is top-ranked in the WordPress Plugin Directory.

Step 1: Install and activate the plugin Code Snippets.

Step 2: Navigate to Snippets in the WordPress admin bar. You will see a list of sample snippets installed by the plugin. These snippets are disabled by default as indicated by the toggle button and are safe to delete.

Screen "All Items" of the plugin Code Snippets.

Step 3: Click the Add New link to create a new snippet. Complete the form and click Save Changes and Activate.

Screen "Add New" of the plugin Code Snippets.

Step 4: Verify that the snippet is working as expected.

Issues

The plugin automatically detects syntax errors in all snippets and prevents these buggy snippets from running. If a code snippet is not working as expected, deactivate it using the toggle button, fix the problem, and try again.

WordPress

Related Tutorials

Open chat
Need help?
Hi! 🤝 Open the chat if you have any question, feedback, or business proposal. I would love to hear from you.