WordPress Functions.php File Explained: The Essential Guide

WordPress functions.php file

The WordPress functions.php file is one of the most important operating files of WordPress. At a minimum, two functions.php files exist for every WordPress website: the functions file in WordPress core and an additional functions file in your WordPress theme. Additionally, a child theme will also have a functions.php file.

In this guide, we’ll cover what the functions.php file is, where the WordPress functions.php file is located, how to edit it and more. We’ll also offer a primer on WordPress functions. Let’s dive in.

If you’re reading this article, chances are that you’ve gotten your basic WordPress installation up and running or already have a WordPress website. Everything seems to be working well, and then you want to do what seems like it should be the click of a button. Then you find a tutorial online, and it turns out you need to directly edit the WordPress functions file, or the functions.php file.

Or maybe you’re building your first WordPress theme and have questions about how the functions file should be edited and best practices for WordPress theme development. At the end of this article, you should have a better grasp of how the WordPress functions.php file works and how to best utilize it.

What Is the Functions.php File in WordPress?

The WordPress functions.php file is a powerful operating file that houses important PHP functions that make a WordPress website functional.

There are two primary locations of a functions file within WordPress:

  • The functions.php file located within the main directory of your website that comes packaged with WordPress core files
  • The functions.php file(s) located in your WordPress theme and/or WordPress child theme. Each WordPress theme you have installed has its own file called functions.php in its root within your WordPress installation.

Quite similar to how WordPress plugins work, the functions.php file simply exists to allow users to enter in custom PHP code to execute on pages. This can be used for countless things, from basic added functionality like server-side calculations, to more complex items, like full programs written in PHP for users to take advantage of.

Note: There is a “functions.php” file in every theme you have. While it’s easy enough to change your blog’s theme with one click via WordPress, keep in mind that this will alter the functions.php file.

If you’re someone who enjoys experimenting with various themes, keep in mind that you’ll need to manually migrate any functions.php changes you’ve made. Additionally, some themes may not support newer versions of PHP if they have not been actively maintained, rendering some of your older functions useless.

That’s why even after you decide to alter your theme, it’s important to formally perform QA or simply test everything on your site. Don’t take WordPress’s message that your theme transition was successful to mean that your functions all transferred over. This must be verified manually.

Where Is the WordPress Functions File Located?

Every theme has its own functions.php file, which can be either a blessing or a curse depending on how often you like to transition themes and the functionality of your site(s).

However, there is a second “overarching” functions file we haven’t yet discussed, too! Let’s take a look at the locations of each of these files and what exactly the differences are between the two files.

The WordPress Core Functions.php File

As the name would suggest, the WordPress core funtions.php file is the “head honcho” functions file. This functions file comes bundled with the WordPress core software that provides the foundational code of a WordPress website.

Within the root directory of your WordPress website, you’ll find this functions file located with the wp-includes folder or directory. Once inside the wp-includes folder, you’ll find the file called functions.php. Examine, but do not change, the contents of this file. Later, you should notice that the structure is a bit different than that of a functions file of a theme.

WordPress core functions file

Note: As with all WordPress core files, it’s wise to not edit the WordPress functions.php file contained in the root directory of your website, regardless of how annoying it is to transfer functions from theme to theme. This functions file is intended for the creators of WordPress to store mission-critical functionality and for advanced WordPress developers.

Why? There are a few good reasons to never edit WordPress core files:

  • Anytime a new version of WordPress is released, the update will overwrite the customizations or changes you’ve made to WordPress core files. Running the latest version of WordPress is always a good idea for security reasons, so you don’t want file customizations to prevent you from running the latest version of WordPress.
  • One space in the wrong spot or an extra semicolon can completely break your WordPress website. Unless you have a thorough understanding of advanced WordPress development, WordPress core files should remain unchanged.

Where Is the Functions File Within My WordPress Theme?

Now, let’s take a look at your theme-specific functions.php file. Unlike the core functions.php file, this file is meant to be edited by you, the developer/webmaster. These files are structured and accessed in such a way that an error typically won’t be site-breaking and will be much easier to debug than an error in the core-level file.

For this, you’ll need to access the wp-content folder containing your WordPress themes. Select the main folder of whatever theme you’re most interested in adding functionality.

WordPress theme functions.php file

Within the root folder of your theme, you should see a functions.php file. Open up this file, but don’t change the contents quite yet. Take a look at this file and note that it has less content than the core functions file.

WordPress Functions.php File Example

In order to give you the best idea of what a proper WordPress functions.php file looks like, we’re providing this “minimalist” template. Essentially, this has the bare minimum code required by WordPress to function. However, you won’t see it doing too much, because the functions haven’t been filled in yet!

The WordPress functions file has “mandatory” functions which will be discussed a bit later on in the article. Without further adieu, here’s our tiny functions.php file (theme-based). This is adapted from the official WordPress theme functions example with some secondary items removed for clarity:

if ( ! isset( $content_width ) )
$content_width = 1000; /* default value type for this is in pixels */

if ( ! function_exists( 'myfirsttheme_setup' ) ) :
/**
* This function is required to register everything later on in the file to this
* theme's functions.php.
*/
function myfirsttheme_setup() {

/**
* Add support for images across your theme/site
*/
add_theme_support( 'post-thumbnails' );

/**
* Enable support for all core WP stuff
*/
add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) );
}
endif;
add_action( 'after_setup_theme', 'myfirsttheme_setup' );

And there you have it! Remember that most themes will actually come with a more filled-in functions file that you’re welcome to add more code to. We recommend always referring to the WordPress developer’s manual when developing within this file.

Note: Before you edit your theme’s functions.php file, it’s imperative that you backup your current website to ensure that you don’t lose any data and hard work you’ve put into them. One way to do this without the pain and heartache is to use a simple WordPress backup plugin like BackupBuddy.

WordPress Functions.php File Security

Warning: The Functions.php file is often targeted by hackers, especially if you have customized code that links directly to your WordPress database (which, for security purposes, you should not).

Before you modify any of these files on your production server, make sure you’re using a decent WordPress security plugin like iThemes Security Pro to ensure that your data is safe.

A plugin like iThemes Security Pro can add file change detection to your website. The File Change Detection feature in iThemes Security Pro will scan your website’s files and alert you when changes occur on your website, including:

  1. Add Files – Malware in the form of spyware could add a malicious file that will record your customer’s keystrokes as they enter their credit card information.
  2. Remove Files – Some malware will remove a legitimate file and replace it with a malicious file of the same name.
  3. Modify Files – Malware will try to hide its malicious code by hiding it in an existing file that it modifies.

Get the bonus content: A Guide to WordPress Security

How Do I Get to the Functions.php File in WordPress?

To get to your functions file(s), you’ll need a few basic pieces of information to perform this process:

  • Your sFTP Credentials (only your host can supply these and likely sent them in an initial email)
  • An sFTP Client of Your Choice
  • The Location(s) of Files(s) You’d Like to Access

Once you have access to an sFTP client and have logged into your server with your credentials, you can browse, view and edit the files of your WordPress site (given you have the right user permissions).

Reference our above discussion on both the “core” and the theme-based functions files.

WordPress Theme Functions Explained

So, now that you’ve gotten a taste of what you might be able to get done with WordPress theme functions, you’re interested in how some of these functions work.

While you should access the official WordPress development guide to see a full and up-to-date listing of the functions you can use, here is a convenient list of useful theme functions and what each one does.

themename_setup() – In this function, simply replace “themename” with the name of your theme. This contains the code to set up your theme.

register_nav_menus() – In this function, we simply set up customized navigation menus. Almost every actual (not theoretical) theme takes advantage of this. Otherwise, every WordPress site’s menu would look exactly the same!

load_theme_textdomain() – This is the function that allows for localization. A panacea for local SEO for WordPress sites that have a wide reach, either nationally or globally, this allows for language and SEO optimization for local regions and areas.

add_theme_support() – Probably the most simple function of the bunch, this one just tells your theme to load up modules that support various types of media. These are all built-in, so it’s not a hassle at all. Many different things, from RSS feeds to video support, are brought in using this function.

And there you have your major WordPress theme functions! However, we’re far from done examining the functions we can utilize in WordPress. Next, we will take a look at major WordPress functions we can utilize that come from the core of WordPress, as we discussed earlier.

Top Five List of Major WordPress Functions

Moving on, here are some major WordPress core functions you should at least be aware of; however, even most WordPress developers need to refer back to the official WordPress Theme Handbook before using each one, mostly due to the complexity of parameters of each function.

Before we start out, feel free to look at all the public functions available to you to use right here in the WordPress code reference.

  1. activate_plugins() – This function piggy-backs off of the activate_plugin() function. Just as its name suggests, it’s a shortcut to activate any installed plugin.
  2. add_menu_page() – This function allows you to very easily and simply add a page to your theme or site’s main menu. Many novice users attempt to do this using raw HTML and CSS. Unfortunately, that approach can mess up a whole theme; it’s always best to take advantage of functions like this one that are made just for that purpose than to try to “hack around it” yourself.
  3. bloginfo() – PHP developers may notice that this looks very similar to a must-have function within PHP itself called “phpinfo()”. This is meant to mimic that function, providing crucial information printed on a page for development use about your version of WordPress, type of server, and more. This is great if you need to quickly grab some specific info.
  4. current_user_can() – This function is a “boolean”, meaning that it’ll return either a “true” value or a “false” value. This is excellent for debugging and can be called to see whether or not you can utilize a specific function. Using the function in question as a parameter, you can check to see if you have the WordPress permissions to access any specific functionality. If you don’t, then you can work on grabbing that before continuing and speed up your development process.
  5. delete_plugins() – This function does quite literally what its name suggests: you put in the registered name of any plugin, and running this function will delete the plugin and its directories. Once again, note that this action is permanent and cannot be reversed. Make sure that you have the right name, you’ve backed up your site, etc., before you continue with executing this function.

Mandatory WordPress Theme Functions

Just as we promised, we’re circling back to take a look at those “mandatory” WordPress theme functions. These are functions that every WordPress theme must have in order to properly function. Note that, just like with most forms of programming, you can simply create a file with these functions blanked out, and this would still be considered valid.

This is a tactic that many blogs use in order to make the process seem much faster than it is and for them to more quickly get you through what they refer to as “the basics of WordPress.” However, please remember that you’re not doing yourself or your site(s) any favors by skipping over these required or “mandatory” functions. You’re simply ensuring that some features that rely on these functions will not work as intended. It’s always best to make your site correctly the first time than to need to continually go back and edit something just to keep everything barely held together!

Simply see the WordPress theme functions initial setup example file to see all of the functions that are “mandatory” for your theme located in the

Editing the Functions.php File

So, now that we’ve gone over the basics of WordPress functions files, let’s take a look at how you might edit this file. There are various “best practices” out there, but the best way to do it is pretty simple.

First, you’ll need to go through the same SFTP client using the same SFTP credentials before. Next, you’ll need to locate and download to your local drive the PHP file itself. Using an editor (such as the open source Notepad++) on your local computer, first edit locally.

Ideally, you should have something installed locally that allows you to test this file. However, if you don’t, just make sure that the original file is backed up and ready for restoration in case your modifications don’t pan out the way you expected them to. Then, replace that file on the server, and watch it run!

As someone likely well on their way to becoming a seasoned WordPress developer, there’s a best practice you should keep in mind as recommended by the WordPress team itself. If you are adding functionality that’s significant in nature that you’d like to be able to reuse (analogous to an “object” in object-oriented language for the programmers out there), it’s a better practice to create a full plugin with this PHP that you can easily install and uninstall on each of your sites. Though this sounds more complex, it actually will save you time over the years.

How Do I Add Code to Functions.php?

If you’re simply referring to modifying this file, take a look at our answer above. However, many users ask this question because they’re interested in actually adding an additional function of their own to the file.

Once again, it’s considered “best practice” to roll features like this into a plug-in instead of endlessly editing theme files. Regardless, we will take a look in the next subsection how to add your own custom functions!

How Do I Inject My Own Custom Function into WordPress?

Now, the time has finally come for you to inject your own functions into WordPress! This is a major milestone for any developer, so a little celebration may be in order. First, let’s dive into exactly how to do this.

Though it’s always best to add your customized code to a plugin you later release, at the very minimum, do not edit your functions on your live site unless you want a disaster on your hands! Always make sure to make a complete backup of your site before editing your theme’s functions.php file.

  • 1. Take advantage of a free plugin to add functions, such as “My Custom Functions” which will be used for this tutorial.

2. After you install the plugin, go to “Appearance” and then go to “Custom Functions” where you will see existing custom functions through this new plugin.

3. You can add a very simple PHP function to this. For example, scroll down to the bottom of the text input box, and you can make something print out “Hello, world!” with the following text:

function hello_world() {
echo "Hello, world!";
}

4. Note that this is simply standard PHP; there isn’t a “WordPress framework” or similar that would fundamentally alter the syntax, which is great news for existing PHP developers.

5. One last quick tip! While you’re working on updating the functions your theme and site(s) have, there’s a file in the wp-config.php. This is found in the root of your theme, and you’ll need to open this up in your favorite text editor through your SFTP client. Find a line that says define('WP_DEBUG', false);; simply change it to say define('WP_DEBUG', true);. Note that you’ll have to change it back to the 'FALSE' once you’re done developing.

How Do I Use Functions.php to Add Scripts to Headers and Footers?

Header and footer scripts are often used for advertising purposes and other tracking purposes. These may need to be inserted on every single page or just on certain, targeted pages. Either way, editing that PHP file directly is not the way to go unless you’re an experienced WordPress user.

Once again, we’ll need to add a small plugin before we get started. This time, the plugin is called the Insert Headers and Footers plugin.

After you add this to your themes or site(s), you’ll be able to access the editing functionality via “Settings” and then “Insert Headers or Footers”. You should see one box for Header scripts and one box for Footer scripts. To clarify, this plugin is expecting you to put in JavaScript, and not PHP, so make sure that your scripts are in the correct format before proceeding, because this plugin will not give you error messages; whatever you put into these boxes will be put onto your pages as JavaScript!

You’ll need to have the scripts you want to add ready to paste right into the boxes, then click “Save”, and the process is done! Some users have reported that these changes aren’t visible until clients clear their browser caches and they clear their server sided cache, so you may need to do this in order for your changes to actually take effect.

Wrapping Up: A Primer on the WordPress Functions.php File

Thank you for taking the time to go over our lengthy (but hopefully worthy!) tutorial on how WordPress’s functions.php file works, the difference between the WordPress core version of the functions file, and the theme version of the file. We hope that all of this information, when assimilated into your day-to-day WordPress workflow and website management practices, can help shape you into an even better WordPress developer!

Get the bonus content: The Ultimate WordPress Maintenance Checklist

WordPress functions.php file
nttnttttnnttttntttnttt”,

“redirect_after_formfill_enabled” : 0,
“redirect_after_formfill_url” : “”,

“exit_popup_enabled”: 0
};

Keep reading the article at WordPress News and Updates from iThemes – iThemes. The article was originally written by Kristen Wright on 2020-11-06 12:35:44.

The article was hand-picked and curated for you by the Editorial Team of WP Archives.

Disclosure: Some of the links in this post are "affiliate links." This means if you click on the link and purchase the product, We may receive an affiliate commission.

Leave a Comment

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

Show Your ❤️ Love! Like Us
Scroll to Top