Add PHP code to WordPress manually and with plugin

Code Snippet is a small piece of code that you can use on your WordPress site and increase the features, capabilities, and performance of the site. It is true that plugins are at the forefront of increasing the performance of WordPress websites, especially for non-experts, but sometimes it is very difficult to find a plugin that can meet your small needs. So learning how to add PHP code to WordPress is essential for anyone working with WordPress. In this article, we will teach you how to add PHP code to WordPress manually and introduce a plugin to make the process easier.

Developers and WordPress reference sites often provide code snippets that you can use on your WordPress website to improve site performance. Adding code snippets to WordPress is optional, and sometimes you can find plugins that do the same thing for you, but sometimes adding a simple snippet is much more effective.

You can convert these snippet codes into shortcodes and apply them anywhere on your site, including posts, tabs, and sidebar widgets. To get acquainted with the concept of shortcode, read the shortcode tutorial article.

Adding code snippets to WordPress is very simple but it is a delicate task and a small mistake can cause your website to fail. So if you need to add a snippet code to your WordPress website, you need to do it correctly and be sure to back up your site beforehand.

In this article, we will teach you how to add PHP code to WordPress manually. Then we introduce the Code Snippets plugin to do this.

Learn how to add PHP code to WordPress manually

The snippet code looks like the following.

add_action( 'after_setup_theme', 'hs_setup' );
function hs_setup() {
    add_theme_support( 'wc-product-gallery-zoom' );
    add_theme_support( 'wc-product-gallery-lightbox' );
    add_theme_support( 'wc-product-gallery-slider' );
}

There are two general solutions for adding snippet code in WordPress:

  • Add snippet code directly to template files
  • Add snippet code in a plugin

Although both of these methods eventually add PHP code to WordPress, there are functional differences between the two methods.

If you add the snippet code directly to your site template, the code entered will no longer work if the template is changed. But if you add the snippet code to a plugin, it will continue to work no matter what theme you use.

So the function you add applies to your current template. Enter the snippet code in the child theme and in the function.php file of your site. But if you need to keep working even after changing the template, add the snippet code to a plugin. In the following tutorial, we will examine both methods for adding snippet code to WordPress.

Add snippet code to functions.php file

WordPress theme is made of template files. Template files define how your website is displayed. For example, the header.php file is a default template used in most WordPress templates to define the header area of web pages.

You can add snippet code to any of the template files such as functions.php, single.php, etc. in one of two ways:

  • Using a child template
  • Add php code directly to existing parent template

It is recommended that you do not customize the template directly from your original template. This means that you do not add PHP code to WordPress directly in the main template. Because when you update your template, all the personalization you did is lost. To maintain personalizations, even after updating the template, you must create a child template and perform the customizations there.

Child Theme is a WordPress template that inherits its functionality from its parent template. If you want to use a lot of snippet code in your template file, it is better to create a child template.

You can create your own template theme child both manually and using plugins.

Adding PHP snippet code is usually done through the functions.php file. In the following tutorial, you will learn more about how to add PHP code to WordPress through the functions.php file.

functions.php is a WordPress template file that allows you to easily extend your WordPress template and installation features. To add features, all you have to do is add a specific snippet code to your function.php file.

Just like any other template file, you can add snippet code directly to your function.php file. But to save the changes even after updating your template, you can create a child template and enter the code into it.

A child template does not affect any of the current operations of your site but allows you to make the desired changes to your site safely and do not have to worry about rewriting them by updating the template.

You can create child templates manually, but you can use the free Childify Me plugin to simplify the child template production process.

Now all you have to do is add the PHP code to the new functions.php file in the child theme. If you have updated your WordPress and are familiar with FTP, do so via FTP.

Otherwise, go to Display »Theme Editor in the WordPress Dashboard and select the functions.php file in the Theme Files menu.

Then paste your snippet code and click the “Update File” button at the bottom of the page. Adding PHP code to WordPress is just that simple.

The downside, however, is that if you change your template, you will lose all the customizations you have made to the child template. This is where the WordPress Snippet code plugin comes in handy.

WordPress snippet code plugins are independent of your template and allow you to add custom code snippets to your site. In the following tutorial, we will explain how to add snippet code to WordPress using plugins.

Use Code Snippets plugin to add php code to WordPress

For code snippets that do not have a specific format, you can use the Code Snippets plugin. In this case, even by updating and changing the format, the snippet code will not be disabled.

Code Snippets plugin is a WordPress plugin that has a graphical user interface for managing snippets similar to the plugin list.

This plugin eliminates the need to embed custom snippet code in the template functions.php file. Because you do not insert any snippet code into your template files, you can keep personalizations on your site even after the template changes.

Using the Code Snippets plugin, you can enable or disable snippets just like plugins.

After activating the Code Snippets plugin, you can add custom snippets and specify a title, description, and tag for yourself. This way you can easily understand the purpose of adding snippets in the future.

Conclusion

One of the features of WordPress that has made it the most popular content management system is the ability to customize it. Code snippets are small pieces of code that if you add to your WordPress website you can increase its capabilities but you need to know how to do it. In this article, we have fully explored how to add PHP code to WordPress.

Shopping Cart