How do i edit php files in wordpress?

If you’re just getting started with WordPress development, one of the most important files you should familiarize yourself with is the functions.php file. There are actually three possible functions.php files that you might use on your WordPress website, and each plays a different role. Knowing where these files live, what their purpose is, and how and when to modify them will help you when troubleshooting issues and give you more control over your site’s functionality.

Show

In this article, we’ll cover everything you need to know about WordPress functions.php files. You’ll learn what functions.php does and how to find and edit it. We’ll even include a few useful code snippets to try out. 

You can start at the beginning and read through or jump to a specific section using the table of contents below:

  • What is functions.php in WordPress?
  • Where is the functions.php file located?
  • How to access functions.php
    • Accessing functions.php in your WordPress Admin Interface
    • Accessing functions.php via FTP
    • Accessing functions.php in your cPanel
  • When should I edit functions.php in WordPress?
  • How to edit functions.php in WordPress
    • Editing functions.php in your WordPress Theme File Editor
    • Editing functions.php with a text editor
    • Editing functions.php directly in cPanel
  • Using a plugin to add functions to your WordPress site
  • How to protect functions.php from security exploits
  • What code can I add to functions.php in WordPress?
  • FAQ
    • Do I need to create a child theme to edit functions.php?
    • How do I restore functions.php from a backup if I don’t have access to my WordPress dashboard?
    • Can I add JavaScript to my functions.php file?

What is functions.php in WordPress?

In WordPress, the functions.php file is where vital code lives that determines the way your site works. There are actually at least two functions.php files in every WordPress website — one included with WordPress core and one included with your theme. If you’re using a child theme, you should also have a functions.php in your child theme folder. 

The functions.php file in WordPress core contains important code that’s required for WordPress to function. You should almost never modify the code in WordPress core’s functions.php file. 

Your theme’s functions.php file is where all the theme-specific features of your website live — like custom menu locations, widget areas, custom header and footer content and functionality, post excerpt length, and more. 

Your theme’s functions.php file behaves in much the same way as a WordPress plugin. The difference is that functions.php should be used for code that is theme-specific, while plugins should be used for functionality that will remain even when you switch themes.

Where is the functions.php file located?

We’ve talked about what the functions.php file does both in WordPress core and in the theme files, but how do you find the functions.php file? Where is functions.php in WordPress? 

All of your WordPress files are located in your site’s root directory and in various subfolders. Your root directory is usually named something like public_html or www, but may have a different name. If you aren’t sure where to find your WordPress installation folder on your server, contact your web host.

Once you’ve found where your WordPress files are located on your server, you can start looking for your functions.php files. We’ve mentioned that there are up to three locations for functions.php: WordPress core, your theme folder, and your child theme folder. In this section we’ll go over where to find each one. 

Where is functions.php in WordPress core?

If you’re looking for the location of functions.php in WordPress core, you’ll find it in the wp-includes folder of your WordPress installation. 

How do i edit php files in wordpress?

While you should never edit functions.php in WordPress core, you may find yourself needing to inspect it for one reason or another. The most likely scenario for poking around in functions.php in WordPress core is if you suspect that malware has been injected into it. 

Before even opening functions.php, you should make a backup of your WordPress site in case you accidentally make a change to the file that causes a critical error. If you find that functions.php has been corrupted or hacked, chances are you may have other areas of WordPress that are affected. The best thing to do is simply replace WordPress with a fresh installation, rather than trying to remove all the offending scripts one-by-one.

Where is functions.php in my WordPress theme folder?

Your theme’s functions.php file is located in the main directory of your theme folder: wp-content → themes → yourtheme.

How do i edit php files in wordpress?

Where is functions.php in the WordPress child theme folder?

The WordPress child theme functions.php file is located in the main directory of your child theme folder: wp-content → themes → yourtheme-child.

How do i edit php files in wordpress?

How do I access functions.php?

Knowing where functions.php lives is only half the job. Now comes the question of how to access functions.php in WordPress. While the functions.php file in WordPress core can only be accessed through secure file transfer protocol (SFTP) or through cPanel, your theme and child theme functions.php files can also be accessed in the WordPress Theme File Editor. 

Accessing functions.php in the WordPress Theme File Editor

If you need to access your theme or child theme’s functions.php file, you can do so directly from the WordPress Theme File Editor. In your WordPress dashboard, go to Appearance → Theme File Editor.

You’ll be taken to the Edit Themes page. From here, under the dropdown next to Select theme to edit, choose your theme and click Select. You’ll see a list of files available to edit under the Theme Files column at the far right of your screen. Click functions.php to open the file.

How do i edit php files in wordpress?

If you’re in your parent theme’s functions.php file, you’ll see a warning notice on the bottom of the screen above the Update File button that says, “Caution: This is a file in your current parent theme.” 

Note: If you’re not the theme developer, you should make your changes in your child theme’s functions.php file to avoid having your edits overwritten in future theme updates.

Accessing functions.php via SFTP

Step 1: Install your SFTP client. If you don’t have an SFTP client already, you’ll need to download one. There are some great free and premium options out there, including: 

Application System Free or Premium
WinSCP Windows Free
Filezilla  Windows, Mac, Linux Free and premium options
Cyberduck Windows, Mac Free
Transmit Mac Premium

In this example, we’ll be using Filezilla. Other SFTP clients should work in a similar fashion.

Step 2: Retrieve your SFTP credentials. To log into your server via SFTP you’ll need the following details:

  • Host (your server’s IP address or url)
  • SFTP username
  • Password
  • Port number

These details can usually be found in your hosting control panel. This information might be generated for you or your host may prompt you to create an SFTP username and password. If you don’t know where to find these credentials or have trouble figuring out how to create them, search your host’s help documentation for instructions or contact their support team. 

Step 3: Enter your SFTP credentials.

Enter your host’s IP address or url, your SFTP username, password, and the port number (usually 22 or 2222, but some hosts may use a different port).

How do i edit php files in wordpress?

Note: You might get a ‘host key unknown’ alert if your SFTP client uses trust on first use (TOFU) authentication. Check ‘Always trust this host, add this key to the cache’ if you plan on using your SFTP client to access the site again. Click OK to proceed.

How do i edit php files in wordpress?

Step 4: Navigate to your website’s root directory.

Once you’ve logged in, you’ll see two file trees displayed at the button of your screen  — one on the left and one on the right. The right side lists the directories on your web server (remote). The left side lists the directories on your computer (local). 

Your root directory is usually in a folder labeled www or public_html, but it might use a different name. If you aren’t sure what folder to look in, ask your hosting provider. You’ll know you’re in the correct folder if you see the wp-admin, wp-content, and wp-includes folders near the top of your file list.

How do i edit php files in wordpress?

Step 5: Navigate to functions.php in WordPress core, your parent theme, or child theme. If you’re looking for functions.php in WordPress core, you’ll navigate to the wp-includes folder.

If you’re looking for functions.php in your theme directory, you’ll navigate to wp-content → themes → yourtheme.

To find your child theme’s functions.php file, go to wp-content → themes → yourtheme-child.

Step 6: Download functions.php. Now that you know where functions.php is located and have accessed it, you might want to inspect or edit the file. Before you can do that, you’ll need to download it. Right click on your functions.php file and select Download. Your file will be downloaded to your local machine.

How do i edit php files in wordpress?

Accessing functions.php in cPanel

If your host uses cPanel, you can access functions.php through its file manager. If you don’t know how to find cPanel in your host’s dashboard, contact their customer support for help. 

Step 1: Open cPanel’s File Manager. Once you’re in cPanel, navigate to the Files section and click on File Manager.

Step 2: Open your website’s root folder. The root folder is commonly called www or public_html, but it may have another name. Root folder naming conventions differ on some hosts, so if you aren’t sure what folder to look in, ask your hosting provider. 

You’ll know you’re in the correct folder if you see the wp-admin, wp-content, and wp-includes folders near the top of your file list.

Step 3: Find and download functions.php. The functions.php file for WordPress core is located in your wp-includes folder. 

Your theme’s functions.php file is located in wp-content → themes → yourtheme.

If you’re looking for your child theme’s functions.php file, you can find it in wp-content → themes → yourtheme-child.

When you’ve found the functions.php file you’re looking for, right click on functions.php and click Download, or single-click functions.php and then click the Download option from the top menu.

How do i edit php files in wordpress?

If you rename your file when you save it, make sure to name it back to functions.php before re-uploading it. And before you make changes to and overwrite functions.php, back up your WordPress site. 

When should I edit functions.php in WordPress?

Before we dive in to how to edit functions.php, we should talk about if and when you should edit it. There are many reasons you might want to edit PHP code in WordPress, but adding that code to your functions.php file is not always the best option and should only be done under certain circumstances, which we’ll discuss below.

When to edit functions.php in WordPress core

Never. You shouldn’t edit functions.php in WordPress core. At most, you might replace it if you find that it’s been corrupted, but deleting or adding code to WordPress core functions.php is likely to break your site. So, you can open it, inspect it, replace it with a fresh copy if you need to, but don’t mess around on any live websites.

When to edit functions.php in your theme

When it comes to your theme’s functions.php file, you may or may not need to edit it. If you’re creating your own theme, you’ll most certainly want to add some custom code to functions.php. Just about every theme has code in its functions.php file that gives it a unique set of features and capabilities. Besides your stylesheets, these functions are a huge part of what makes one theme different from another.

If you’re adding code to your theme’s functions.php file that isn’t specific to your theme and could be used independently of your theme, consider making it a plugin instead. This way you can use that feature across multiple themes and even submit it to the WordPress.org plugin library where it might benefit other WordPress users. 

Before creating your plugin, check the plugin library to make sure what you’re creating doesn’t already exist. If a quality solution exists already, you might want to save your development energy for a different custom feature on your site.

When to edit functions.php in your child theme

If you’re using a theme from a developer, and want to modify the functions.php file, you should always use a child theme to make those changes. When theme developers issue a new version of their theme and you update to the latest one, your current theme is overwritten by the new files — including functions.php. 

Child themes are unaffected by parent theme updates, so you’ll retain all your custom code if you make changes in a child theme’s functions.php file.

Before adding code, consider whether using a plugin would be a better solution. Is the code you’re adding specific to your theme? Edit your child theme’s functions.php file. Is the code you’re adding something you want to still have if you change themes in the future (e.g. Google Analytics tracking code or Facebook pixel)? Use a plugin.

How to edit functions.php in WordPress

You should always take steps to protect your site from any mistakes you might make while editing functions.php. So before you start making edits, back up your site and consider creating a staging site to test them first. This way your live site is not disrupted if you accidentally add some code to functions.php that causes major issues or — even worse — crashes your site completely.

Now that you’ve backed up your site, you can move on to editing your functions.php file.

How to edit functions.php in the WordPress Theme File Editor

Editing functions.php in the WordPress Theme File Editor is a little risky. If you’re using this tool because you don’t have access to cPanel or SFTP, then you’ll be in a pretty bad situation if your edits cause a fatal error that prevents you from accessing your WordPress dashboard. 

If you want to edit functions.php in the Theme File Editor, make sure that you have direct access to the server and your backups so you can restore your backup file quickly.

You can find the WordPress Theme File Editor under Appearance → Theme File Editor in your WordPress admin panel. 

You’ll be taken to the Edit Themes screen, where you should select your child theme, then select the functions.php file from the Theme Files column at the right of your screen.

How do i edit php files in wordpress?

If you’ve selected the parent theme instead, you’ll see a warning at the bottom of your screen that says, “Caution: This is a file in your current parent theme.” You don’t want to make edits to your parent theme’s functions.php file because your changes will be overwritten the next time you update your theme.

If you see functions in your file that you don’t understand, WordPress includes a handy documentation lookup feature just above the Update File button. Click the dropdown and search for the function name that you want to learn more about and click Look Up. You’ll be directed to the WordPress.org documentation for that function. 

When you’re done with your changes, click Update File. Check the front and back end of your site to make sure everything is still working as intended. If you encounter a fatal error and need to restore functions.php from your backup, you can do that in cPanel or via SFTP.

Editing functions.php with a text editor

The best way to edit functions.php is in your code editing software of choice. Since you’re editing a copy that’s offline on your local computer, you don’t have to be connected to the internet to work on your file. You’ll also have more control over when you commit your changes to your live site. 

You can edit your code whenever you like and upload your changes at a time when your website normally gets the least traffic. That way, if there are any issues, the fewest number of visitors will be impacted.

Step 1: Open functions.php in your text editor. If you aren’t familiar with using a text editor, there are several free options to choose from. 

Free source code and plain text editors:

Application System
Notepad++ Windows
Atom.io Windows, Mac, Linux
Sublime Windows, Mac, Linux
TextEdit
*make sure you’re in plain text mode
Mac (default application)
Notepad Windows (default application)

Step 2: Edit functions.php and save to your local machine. Your display may look different depending on what program you use to edit your functions.php file. This is what PHP code looks like in atom.io:

How do i edit php files in wordpress?

Add or change whatever information you need to, then save your file. 

Step 3: Log into your web server via SFTP or cPanel. Navigate to your theme or child theme folder and find the functions.php file.

Step 4: Change the file name of functions.php on your web server. In case your edited version contains errors, you don’t want to overwrite your original functions.php file. You’ve made a backup, of course, but instead of having to pull the functions.php file from your backup, you’ll have your original file on-hand. 

You want to be able to restore the functions.php file quickly if something goes wrong, so renaming it to something like ‘functions-orginal.php’ will make sure that the contents of the original file are preserved on your server.

How do i edit php files in wordpress?

Step 5: Upload functions.php from your local machine to your webserver. Using SFTP or cPanel, upload your functions.php file to your theme or child theme’s main directory.

Step 6: Visit your website to make sure it’s working correctly. Once you’ve uploaded your functions.php file, you should check to make sure that everything is working on your site. Visit the front end and log in to the WordPress dashboard to make sure everything is accessible and displays and functions correctly. 

Step 7: Delete the old, renamed functions.php file. Once you’ve established that your site is working properly, you can delete functions-original.php (or whatever you renamed it to). If you find an issue down the road, you should still have your functions.php file from the backup you made.

Editing functions.php directly in cPanel

If you don’t have or don’t want to use a source code editor, you can edit functions.php directly in cPanel (if your host uses cPanel). Like editing in the WordPress Theme File Editor, it’s a little more risky than working on your local machine. If your internet connection is disrupted while you’re editing, you could lose your changes. You also run the risk of potentially overwriting your original functions.php file.

Step 1: Find functions.php in cPanel. In cPanel, click on File Manager.

Navigate to your root folder (usually public_html or www, but it could have a different name).

Step 2: Make a copy of functions.php. In your File Manager main menu, click + Folder to add a new folder. You’ll want to make a copy of your functions.php file and save it here as a backup.

Name your new folder something easy to identify like ‘backup functions’ so you can easily find it later. Click Create New Folder.

How do i edit php files in wordpress?

Step 3: Find functions.php in your theme or child theme folder and copy it to the backup folder. In your theme or child theme folder, scroll down to functions.php and right click on the file name. Select Copy.

How do i edit php files in wordpress?

A dialog box will appear asking you to enter the path you want to copy the file to. The path to the folder you’re currently in will be pre-populated, so if the file path to your backup folder differs from what you see, type in the correct path. Click Copy File(s).

Step 4: Navigate back to functions.php in the theme or child theme directory. Now that you have a backup, you can go back to the functions.php file in the theme or child theme directory and start editing. 

Right click on functions.php, then click Edit.

Step 5: Edit functions.php. You should now see the contents of your functions.php file on your screen. It should look something like this: 

How do i edit php files in wordpress?

Add or change whatever information you need, then save your file. 

Using a plugin to add functions to your WordPress site

Instead of directly modifying the functions.php file, you can use a plugin like Code Snippets to add functions to your website. If you need to add a fair number of functions to your site, it can be easy to lose track of which functions you still need and which you don’t. 

Code Snippets provides a graphical user interface (GUI) for managing your blocks of PHP code. You can add descriptions so that you know what the code is supposed to do, and turn blocks of code on and off, much like activating and deactivating a plugin.

Since you’re not making changes to functions.php, it’s a much safer method of adding PHP functions to your WordPress site.

How to protect functions.php from security exploits

The functions.php file is often a target of hackers, so keeping it safe should be a high priority. Take the following steps to help you secure your functions.php file:

1. Install a security plugin

By using a security plugin, you’ll enjoy advanced protection from hackers across multiple areas of your site. 

Jetpack Security is an inexpensive option that helps keep your site safe from malicious attacks. It includes malware scanning and one-click fixes, brute force attack protection, downtime monitoring, an activity log so you know if and when your site was hacked, and login protection via two-factor authentication (2FA).

How do i edit php files in wordpress?

Jetpack Security also monitors your site for any changes to core WordPress files, outdated or insecure plugins, and other vulnerabilities so that you can catch them before a hacker finds them and takes advantage. 

Plus, it provides real-time WordPress backups so you can quickly restore a clean version if anything goes wrong — even if you can’t log into your site.

2. Deny access to WordPress core functions.php via the .htaccess file

If your website is hosted on an Apache server, you can use an .htaccess file to help protect your WordPress core functions.php file by blocking access to the entire wp-includes folder.

Step 1: Download .htaccess

Using SFTP or cPanel, navigate to the root folder of your website. Right click on .htaccess and select Download to download the file to your local computer.

Step 2: Add code to block access to the wp-includes folder

In a source code or plain text editor, open .htaccess and add the following code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
</IfModule>

Save your file. Make sure that your editor program does not include a file extension. If you’re using Notepad++ or Text Edit, it may add a .txt file extension. The file should simply be named ‘.htaccess.’ 

Step 3: Upload the edited .htaccess back to the root folder

Upload your edited .htaccess file back to your website’s root folder. You’ll be asked if you want to overwrite the existing file. Click Ok.

Now your .htaccess file should block external access to any files in your wp-includes folder, including functions.php.

3. Deny access to functions.php using functions.php

You can block direct access to your functions.php file by adding the following code snippet directly to functions.php.

if (!empty($_SERVER[‘SCRIPT_FILENAME’]) && ‘functions.php’ ==
basename($_SERVER[‘SCRIPT_FILENAME’]))
{
die (‘Access denied!’);
}

4. Hide functions.php outside of the root directory

Hackers know the default location of all standard WordPress files. Changing the location of sensitive files to a restricted access folder outside the root directory can keep it out of reach of hackers. This process is a little more involved than just adding some code to your .htaccess file, but it’s worth the extra effort for the additional layer of protection to your website. 

In just a few steps, you can secure functions.php outside of the root folder.

Step 1 : Download your current functions.php file

If you’re moving your theme’s functions.php file, you’ll find it under wp-content → themes → yourtheme. Your child theme’s file will be in wp-content → themes → yourtheme-child.

Once you’ve found your functions.php file, download it to your computer via SFTP or cPanel.

Step 2 : Create a new directory outside of the root folder

Navigate outside of your root directory to the next file folder level up. You should see the name of your root folder as well as several other folders. In this directory, right click and select Create directory.

How do i edit php files in wordpress?

Give your directory a name that’s easy to remember as the location of your functions.php file.

Step 3: Upload functions.php to your new folder

Upload the functions.php file you downloaded earlier into your new folder via SFTP or cPanel. 

How do i edit php files in wordpress?

Check your new folder and functions.php file permissions and make sure they’re set to 600.

How do i edit php files in wordpress?

Step 4 : Point WordPress to the new functions.php file

You should have a functions.php file in your new directory as well as the original functions.php file that is still in your theme folder. In order for WordPress to find and use the correct file, you’ll want to delete all the information in your theme file’s functions.php file and the following code snippet:

<?php
include('/home3/usr/securefunctions/functions.php');
?>

Note: Your file path will look different, as you’ll be using your server’s directory names.

In cPanel, you can open the root directory’s functions.php file and edit it directly or you can edit the copy you downloaded to your local machine earlier using a source code editor. For this example, we’ve edited the copy we downloaded earlier using Atom.io.

Save your funtions.php file and upload it back to your theme directory.

How do i edit php files in wordpress?

You’ll be asked if you want to overwrite the file. Click Ok.

Now, WordPress should reference your new functions.php file in its secure location outside of the root folder.

4. Change the name of your functions.php file

Changing the name of your functions.php file is another step you can take to keep it safe from hackers. You’ll need to host this file outside of your root folder, so if you’ve followed the process in Hide functions.php outside of the root directory, you’ll only need to take a few additional steps.

Step 1: In your secure directory outside the root folder, change the file name of functions.php

Go to the functions.php folder hosted outside of your root directory and change the file name to something unique, like ‘keep-out.php’ or ‘these-are-not-the-functions-you-are-looking-for.php’. Okay, that last one’s a little long, but it doesn’t really matter what you call it as long as you include it in the file path in the theme folder copy of functions.php.

Step 2: Edit the code in functions.php in your root directory to reflect the name change

Now that you’ve changed the name to something other than functions.php, you’ll need to make sure your theme directory copy of functions.php is pointing to the right file name. 

Download the root folder copy of functions.php to your local computer and edit it with your text editor. Change the functions.php file name to your new file name, then save your file.

<?php
include('/home3/usr/secureconfig/keep-out.php');
?>

Step 3: Upload functions.php back to the theme directory

You’ll be asked if you want to overwrite the existing file. Click Ok

Now WordPress should point to your renamed functions file. 

Additional layers of security for functions.php

You can add some additional protection to your website as a whole, including your functions.php file, by making similar changes to your wp-config file like renaming it, hosting it outside the root directory, and denying public access.

If you see suspicious code in your functions.php files and are worried you may have been hacked already, you’ll want to read up on how to detect and remove malware from your WordPress website.

What code can I add to functions.php in WordPress?

There are a lot of different code snippets that you can add to your WordPress functions.php file. However, you’re often better served by using a plugin (or writing your own plugin) for many functions. If the code you want to add is specific to your theme, then you can add that PHP code to your child theme’s functions.php file. 

Below are a handful of useful functions you might insert into your child theme’s functions.php file: 

Adding a new admin user in functions.php

If you’ve lost admin access to your WordPress dashboard and can’t recover your username or password via email, you can create a new admin user through your hosting account. Either via SFTP or cPanel, you can download your functions.php file and add the following code to create a new admin user for your site.

function qode_add_new_admin_account() {
$user = 'your-username';
$password = 'your-password';
$email = 'your-email';
if ( ! username_exists( $user ) && ! email_exists( $email ) ) {
$user_id = wp_create_user( $user, $password, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
}
add_action( 'init', 'qode_add_new_admin_account' );

In the above code, replace ‘your-username’, ‘your-password’, and ‘your-email’ with your own credentials. Save your functions.php file and re-upload it to your theme folder. This will create a new admin user with the username, password, and email address that you’ve assigned in the code. 

After you’ve uploaded your file, navigate to your site’s login page and use your new username and password to log in. Once your login is successful and you’ve confirmed that you have admin privileges, you should remove the code that you added to create your new admin account from functions.php. 

Adding and removing image sizes in functions.php

When you upload an image to the WordPress Media Library, multiple variations of your image are generated. WordPress has default thumbnail, small, medium, and large sizes that are used in addition to your full size image. Your theme will generally pull the optimal image size for individual parts of your site. 

If you’re creating your own theme or need to make modifications to a child theme to accommodate additional custom image sizes, you can do so in your functions.php file. 

Use the following code and change your custom image size name from ‘custom-small-square’ and ‘custom-landscape’ to whatever name you’d like to use. The numbers next to your custom image size name represent the image size in pixels, so enter the pixel values with width first and height second.

function register_custom_image_sizes() {
if ( ! current_theme_supports( 'post-thumbnails' ) ) {
add_theme_support( 'post-thumbnails' );
}
add_image_size( 'custom-small-square', 450, 450, true );
add_image_size( 'custom-landscape', 1000, 600 );
}
add_action( 'after_setup_theme', 'register_custom_image_sizes' );

Keep in mind that additional custom image sizes can weigh down your site and impact load times. If you have some WordPress default image sizes that your theme doesn’t use, you may want to delete and disable those to make room for your custom sizes. 

If you want to disable WordPress default image sizes, you can place this code snippet into your functions.php file:

add_filter( 'intermediate_image_sizes_advanced', 'prefix_remove_default_images' );
// This will remove the small, medium, and large default image sizes. 
function prefix_remove_default_images( $sizes ) {
 unset( $sizes['small']); // 150px
 unset( $sizes['medium']); // 300px
 unset( $sizes['large']); // 1024px


 return $sizes;
}

If you only need to disable one or two sizes, you can only add those sizes to the code above and delete the code that you don’t need. 

Use functions.php to allow additional file types in uploads

WordPress only allows a few different file types to be uploaded in your WordPress dashboard. This is a reasonable security measure, but you might encounter a situation where you need to upload a file type that’s not natively allowed. 

To add more allowed file types to WordPress, add the following code to your child theme’s functions.php:

function additional_upload_file_types($mime_types){
$mime_types['stl''] = 'application/sla';
$mime_types['stl'] = 'application/vnd.ms-pki.stl';
$mime_types['stl'] = 'application/x-navistyle';
return $mime_types;
}
add_filter('upload_mimes', 'additional_upload_file_types', 1, 1);

For the above example, we used the stl file type, which is a CAD file type. It’s an unusual file type to upload to a website outside of certain niche industries, but if your site is for an industrial designer, engineering firm, or even an orthodontics lab, you might find the ability to upload STL files really useful. 

You can replace the $mime_type variable with whatever file extension suits the needs of your website. You’ll also need to include the media type and subtype after that (e.g. ‘application/sla’ or ‘image/octet-stream’). Here’s a complete list of mime types that you can refer to. 

Frequently asked questions about editing the functions.php file in WordPress

We’ve covered what the functions.php file is, how to find functions.php, how to access it, how to edit it, and a few code snippets you can add to your functions.php file. We’ll wrap things up with a few helpful answers to frequently asked questions. 

Do I really need to use a child theme to edit functions.php?

The short answer? Yes.

If you’re a theme developer, you’ll definitely want to add your theme-specific functions directly to your theme’s functions.php file. If you’re modifying a theme from another developer that will get regular updates, however, you’ll absolutely want to use functions.php within a child theme. 

Most theme developers will periodically make updates to their themes for security and performance reasons, or to add new features. If you change the parent theme’s functions.php file, those changes will all be overwritten when you update to your theme’s latest version. All your hard work and customizations — poof! Gone. 

Child themes aren’t touched when the parent theme updates, so if you make changes to your child theme’s functions.php file, your changes will be preserved any time you update your parent theme.

I edited functions.php and now I don’t have access to my WordPress dashboard. Help! 

In the unfortunate event that you edited the functions.php file and caused a critical error on your site that kept you from accessing the WordPress dashboard, don’t worry — all is not lost!

There are a few things you can do to restore access to your site. You can edit functions.php via SFTP and a text editor or directly in cPanel if you know which bit of code caused the issue. Alternatively, you can restore functions.php from a backup or download a fresh copy of functions.php from your theme developer. 

I know what code I messed up. I just need to access and edit functions.php to fix it.

See How to edit functions.phpabove for instructions on how to access and edit your theme or child theme’s functions.php file.

I have no clue what I did to my functions.php file, but I have a backup.

If you have a backup on hand, you can access your files either on your web host’s server or your remote server (depending on where you have opted to store these files), and use them to replace your corrupt functions.php file via SFTP or cPanel. 

If you feel more comfortable restoring your entire site backup rather than trying to restore just the functions.php file, our article, “How to Restore WordPress from a Backup” goes in depth on the different methods you can use to restore your WordPress site.

I have no idea what code caused the problem and I didn’t make a backup.

If you don’t have a backup, you can download a fresh copy of the theme you’re using from your theme developer, extract the functions.php file, and overwrite the broken functions.php file on your server via SFTP or cPanel.

Note: If you’re using a theme that hasn’t been updated in awhile, you may encounter issues in overwriting functions.php with a version that’s from a much more recent update. If you encounter issues, you might be better off just manually updating your entire theme to the latest version. Alternatively, you can contact your theme developer to get the theme files for your current version and use that version of functions.php.

Can I add JavaScript to my functions.php file?

Yes, you can add code to your functions.php file that will add your custom JavaScript to either specific pages, posts, or all pages and posts on your site. This article covers all the different ways you can add JavaScript to your functions.php file.

This entry was posted in Learn. Bookmark the permalink.


Rob Pugh

Rob is the Marketing Lead for Jetpack. He has worked in marketing and product development for more than 15 years, primarily at Jetpack, Mailchimp, and UPS. Since receiving a Master of Science in Marketing Degree from Johns Hopkins University, he’s focused on delivering products that delight people and solve real problems.

Explore the benefits of Jetpack

Learn how Jetpack can help you protect, speed up, and grow your WordPress site.

Get up to 60% off your first year.

Compare plans

Where do you edit php for WordPress?

php in the WordPress Theme File Editor. If you need to access your theme or child theme's functions. php file, you can do so directly from the WordPress Theme File Editor. In your WordPress dashboard, go to Appearance → Theme File Editor.

How do I access WordPress php files?

Open your FTP tool and enter your hosting credentials to connect to your site. To find the right file, navigate to wp-content/themes/[the name of your theme]. When you open this folder, you'll see the functions. php file.

Can you edit php file?

A PHP file is a plain text file, so you can open it in any text editor like VI, Notepad, or Sublime Text. For beginners, tools like Notepad++ should do, since they'll just be running small snippets of code.

How do I edit Wpress files?

Click into the theme directory of the template you're using. This is the theme you found earlier in the WordPress Appearance section. To open the File Manager editor, select the file you want to edit and click “Edit.” A new window will appear allowing you to select the editing method you wish to use.