How to Safely Add Code Snippets to Functions.Php File in WordPress?

add-custom-code-snippets-to-wordpress-functions-php-file-themaverickspirit Pin It

If you have a WordPress website, then I am sure you might have come across a situation where you need to add a custom code snippet to the functions.php file.

Well, this is the WordPress Tutorial post that will guide you on – How to Safely Add Code to functions.php in WordPress website?

siteground-web-hosting-review-wordpress-banner

Before moving on to the steps to edit the funtions.php file, let’s take a look at what functions.php is!


What is functions.php?

Functions.php file is a PHP file that is present in each WordPress theme. It behaves like a WordPress plugin and used to define functions, classes, actions, and filters in the theme.

In simple words, functions.php is used to add and extend features and functionality of a WordPress website.

Functions.php gets automatically loaded in the back-end and front-end of any WordPress website.


Remember Functions.php Is Also Dangerous!

Yes, It is dangerous!

As a small mistake in the functions.php can make your WordPress site inaccessible.

Therefore, the very first thing that I will recommend you, is to take a backup of your functions.php file or a backup of your WordPress website.

To take the backup of your WordPress website you can refer to some of our recommended plugins –

Best WordPress Backup and Restore Plugins to Secure Your Website!


How To Add Custom Code?

I am enclosing three methods using which you can add custom code snippets to your theme’s functions.php file safely – 

  1. Use the Code Snippets Plugin
  2. Manually Add The Code To Your Theme Functions.php From WordPress Dashboard
  3. Manually Add The Code To Your Theme Functions.php From cPanel (Recommended)

Method #1 – Use the Code Snippets Plugin

What Code Snippet WordPress Plugin does? Code Snippets WordPress Plugin lets you easily add code snippets to your WordPress site.

Install Code Snippets WordPress Plugin and activate it.

Also, Check Out How to Install WordPress Plugin? [Beginners Guide]

Once the plugin is activated, it creates a new menu item labeled “Snippets” into your WordPress dashboard.

Steps To Add Code Snippets –

1. Go to your WordPress dashboard and click on Snippets.

go-to-snippets-from-wordpress-dashboard

2. Click on Add New. You will be redirected to ‘Add New Snippet’ page where you need to fill in the following details –

add-new-code-snippet
  1. Title – Name of the snippet.
  2. Code – The code snippet you want to add to your website.
  3. Where to run this code –  You can choose where you want to run this code.
    1. Run snippet everywhere
    2. Only run in the administration area.
    3. Only run on-site front-end.
    4. Only run once.
  4. Description – The description of the snippet.
  5. Tags –  Tags related to the snippet.

3. Finally, you can click on the ‘Save Changes and Activate’ button and activate the code.

4. If you just want to save the snippet without activating it, then you should click on the ‘Save Changes’ button.


Method #2 – Manually Edit Functions.php from WordPress Dashboard

Follow these steps to add the code snippets to functions.php manually from the WordPress dashboard –

  1. Go to your WordPress dashboard > Appearance > Editor.
  2. You will be redirected to ‘Theme Editor’ page where you can edit your WordPress theme files.

First, you need to select the theme your website is using from the dropdown and after that, you need to locate the file “Theme Functions” or “functions.php” and click it.

  1. You will see a file open on the left of the list of files.
  2. Scroll down the way to the end of the file, paste the code and save the file.

Method #3 – Manually Add the Custom Code in Functions.php from cPanel

Below are the steps to add the code snippets to functions.php manually directly from the cPanel –

  1. Open your cPanel > File Manager > Go to public_html folder > [WordPress Folder] > wp-content > themes > [theme folder] > functions.php.
  2. Right-click on the functions.php and edit the file.
  3. Scroll down the way to the end of the file, paste the code and save the file.

Note I will recommend this method as the changes made can easily be reverted if the website crashes.


Wrapping Up

I hope this post gave you an idea about how to safely add the custom code snippets to the functions.php file in your WordPress website.

In case you have any trouble just comment down the issue below.

1 Comment on “How to Safely Add Code Snippets to Functions.Php File in WordPress?

  1. I am the creator of a plugin that adds functionalities for adding code into wordpress in a per-page or per-post basis.

    https://wordpress.org/plugins/ultimate-shortcodes-creator/

    It can be done via shortcodes or Gutenberg Blocks. It’s lightweight and no overload is paid if the shortcode is not present in the page.

    Code can include php, js, css, and even ajax code. Also, scripts code can be inserted via or footer.

    May be you can consider it.

    Best regards,

    César.

Leave a Reply

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

12 Shares 6.3K views
12 Shares
Share via
Copy link