5

What Is the functions.php File in WordPress?

 1 year ago
source link: https://code.tutsplus.com/articles/what-is-the-functionsphp-file-in-wordpress--cms-93254
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

What Is the functions.php File in WordPress?

Every WordPress theme comes with a function.php file or the theme functions file. In this post I'll explain where to find it, and why it's better to not modify it directly.

Though each theme has its own functions.php file, it is only the active theme's functions.php file that is activated when the site loads.

Inside functions.php is where you can define the custom (PHP) code snippets for adding new features to the site or changing default ones. With functions.php, you can do things such as the following:

  • insert header and footer scripts
  • specify a custom message to be shown on your WordPress dashboard
  • use WordPress hooks like excerpt_length (to change the length of your post excerpt from the default word count)
  • include features like post thumbnails, nav menus, and so on, through the use of add_theme_support()
  • specify functions to reuse in several theme template files and many more

Once added, the custom feature will be immediately applied to the site's theme and you'll see changes the next time you load your site.

Where to Find functions.php?

The functions.php file is found in the /wp-content/themes/ folder within your WordPress installation directory, inside the folder for the currently active theme.

You can edit the file and add code snippets for whatever custom feature that you want to add to your site. However, this is something that should generally be avoided. functions.php is a part of the theme, and if you edit it in a third-party theme, there are several risks. For one, you risk losing file changes when you either update the theme or change it. Worse yet, an error in the code could lead to a critical website error, effectively locking you out of your site.

If you do want to edit the functions.php file for a third-party theme, the best thing to do is to create a child theme. Other than that, simply avoid editing the functions.php file. Read more about child themes and overriding functions.php here on Envato Tuts+.

There are other safer ways to add code snippets to your site's theme functions file. I'll discuss one of them in the next section.

Recommended Way to Add Custom Snippets to functions.php

Instead of directly editing functions.php, use a code snipper plugin like WPCode to easily add custom features to your WordPress site using code snippets, without touching your theme’s functions.php file.

The plugin allows you to add your own code snippets, and in addition, provides you with a built-in library of code snippets from which you can find some of the most popular snippets to use on your site, saving you the hassle of downloading separate plugins.

Code Snippets Library

Code Snippets Library

Code Snippets LibraryCode Snippets Library

You can also search for other ready-made plugins by categories, which are listed in alphabetical order.

Under Code Snippets, you'll find the list of code snippets already activated for your site. To add a new snippet, click the Add New button.

Add New Snippet

Add New Snippet

Add New SnippetAdd New Snippet

You'll be led to the Add Snippets page to either select one of their pre-made templates or start from scratch with a blank snippet.

Choose pre-made template or start blank snippet

Choose pre-made template or start blank snippet

Choose pre-made template or start blank snippetChoose pre-made template or start blank snippet

If you choose a new (blank) snippet, you'll be led to the page shown below. There you'll provide the name of your code snippet in the title box and then copy your code snippet into the code block. You'll also need to specify the code type (should be PHP)

Paste in code snippet

Paste in code snippet

Paste in code snippetPaste in code snippet

Click the Save Snippet button when done. The code snippet will be added to your theme functions file and applied to your site's theme.

Summary

Every WordPress theme comes with a functions.php file which is used to add custom features and functionalities to the site's current theme as well as change default features.

Generally, you should avoid modifying functions.php for a third-party theme. This is to avoid losing useful file content (when you change or update the theme) and instigating a critical error in your site. Instead, use a code snippet plugin like WPCode to add custom snippets.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK