Introduction:

Surfing the Internet for solutions to WordPress problems will inevitably bring you to sites that ask you to insert some piece of code or the other into functions.php. If you’re new to WordPress, this can be a confusing thing.

By default, WordPress doesn’t have any obvious interface for you to insert code. And besides…copy pasting stuff into your installation is dangerous.

Here you will come to know how to do it.

What is functions.php

functions.php or the theme functions file is a template used by WordPress themes. It acts like a plugin and gets automatically loaded in both admin and front-end pages of a WordPress site. Usually this file is used to define functions, classes, actions and filters to be used by other templates in the theme. It can be used to add features and extend the functionality of both the theme, and the WordPress installation.

The functions.php file can be found in your theme’s folder. You can add both built in WordPress functions and regular PHP functions to hooks and filters that are predefined throughout the WordPress core.

Although every theme you have installed on your site has its own functions.php file, only the active theme’s file will run its code. If your theme doesn’t have a functions.php file you can simply create a plain-text file named functions.php and add it to your theme’s directory.

Child themes can have their own functions.php files which can be used to either build on top of or entirely replace the one in the parent theme directory. Some of the things you can do with a functions.php file include use WordPress actions and filters, you can enable post thumbnails, post formats, and navigation menus.

Problems /Risk in Functions.php:

Despite its value, functions.php is a dangerous file. The reason is that it’s linked to your theme. If there’s a syntax error in the code, it could disable the theme itself and make your site inaccessible. So be careful! We’ll also show you how to avoid pitfalls when inserting code.

Step:1 Locate the functions.php in your theme.

Log into your WordPress dashboard and go to “Appearance->Editor” as shown here:

wordpress

This will bring you to a page where you can edit your theme files. These files are listed on the right-hand side. Your current theme is selected by default. Locate the file labeled “Theme Functions” and click it as shown here:

wordpress

Step 2: Making Sure There’s No Closing ?> Tag

This will open up functions.php in the textbox on the left-hand side. Scroll all the way down. If you see the following symbol at the very end of the file, delete it:

?>

The old way of doing things was to ensure that every php file ended with a ?> tag. But this led to problems where people would accidentally leave a blank line or even a space afterward, and it would crash their site. It was also very hard to debug.

So before you modify functions.php, ensure that it doesn't end with ?> . If it doesn't remove it.

Step 3: Formatting your Code

Older sites, or those adhering to older WordPress PHP standards might make a few mistakes in their code. So copy the code from their site, paste it into a text editor like Notepad and make the following changes:

Remove any opening <?php code
Remove any trailing ?> code

Basically ensure that the code you’re copying is not enclosed in <?php and ?> tags. If it is, you’re going to have problems and your site will crash because we removed the closing ?> tag in Step 2.

Once your code is clean, we’re ready to paste!

Step 4: Pasting the Code into functions.php

Scroll all the way down to the textbox Step 1 with “functions.php” selected on the right-hand side. Now paste your code at the very bottom as shown here:

To reiterate (because it’s so important), make sure there is no closing ?> tag! Now save your changes and you’ve successfully added code to functions.php in WordPress! Here’s the output of the code we just added in this step as an example:

Access from cPanel File Manager

Even though We’ve shown you how to access and modify functions.php from inside WordPress, we suggest you do it from your cPanel’s file manager instead. Locate your WordPress installation, and go to the following location:

[WordPress Folder]->wp-content->themes->[theme folder]->functions.php

You can now right-click and edit functions.php from here. The reason why we recommend doing this is so that you can revert the changes if your site crashes!

Use a Custom Plugin for WordPress Code

This requires a separate tutorial by itself. But the idea is that you create your own separate plugin for WordPress specific code instead of inserting it into functions.php. This way if something goes wrong, WordPress can just disable the plugin instead of crashing your site!

We hope this was a useful tutorial for you on how to safely insert code into functions.php in WordPress!

You can know about increase the php memory upload size in word press click here.