👋🏼Welcome to my WP-Host blog where I am excited to share my knowledge and expertise on WordPress hosting and website construction tutorials with you. Let’s connect and learn from each other! You can reach me at info@yrshare.com.
WordPress function file Functions.php tutorial guide (super detailed): This article is a guide about Word Press functions, the main content includes:
Introduces the concepts and functions of Word Press functions, and how to use functions to expand and customize Word Press websites.
The commonly used Word Press functions are explained in detail, including Word Press core functions, theme functions, plug-in functions, etc.
Classified and summarized Word Press functions, such as database functions, user functions, mail functions, query functions, etc., which are convenient for developers to find and use.
It focuses on some commonly used Word Press functions, such as wp_head(), wp_footer(), wp_enqueue_script(), wp_enqueue_style(), etc., and how to use them to add styles and scripts.
All in all, this article provides a systematic guide to Word Press functions, which is of certain reference value for developers who are learning or using Word Press.
Many effects of the wordpress website can be achieved by modifying the functions.php file. For example, many code snippets shared by WP-Host are added to functions.php. So we will talk about the functions.php file in detail in the wordpress tutorial we will share next.
Table of Contents
What is the functions.php file?
The functions.php file for Word Press is included with all free and paid Word Press themes. To the untrained eye, it may not seem like much, but function files are a powerful tool that allow you to do a lot of interesting things.
The feature file is described in the WordPress Codex like this:
You can use it to call functions, including PHP and the built-in Word Press, and define your own functions. You can produce the same result by adding code to a Word Press plugin or through a Word Press theme functions file.
In simple terms, function files enable you to add custom code to your site. It allows you to create new functions or reference existing functions in a custom way. As Codex points out, this makes function files very similar to plugins, with some differences.
The most important difference is that function files belong to a specific theme. If you change the theme or update to a newer version, your changes will disappear. For this reason, you should consider creating a child theme and adding the new code to the child theme’s functions file. This way, you can update the parent theme without losing your changes.
Whether you choose to use a function file or create a plugin is entirely up to you, depending on your needs. Now let’s look at the different ways of editing function files.
How to edit function files
Editing function files is as easy as using a standard text editor such as(Text Edit or Notepad). Before starting, it’s important to create a backup of your site and save the original, unedited functions.php file. This will allow you to restore your site if something goes wrong during editing.
1. Use the Word Press editor
If you have access to the Word Press admin interface, you can edit the features file directly from the theme editor. Go to [Appearance > Editor].
On the right side of the screen, you can see a list of all files included in the theme. These vary depending on the theme you use, but one of the most important options should be Theme Functions (functions.php). Just click on the file to open it in the editor.
Now you can directly edit the file. Don’t forget to click update file at the bottom to save your changes when you’re done.
2.Access files via FTP
If you don’t have access to the admin dashboard or prefer to configure files directly, you can also use FTP tools like FileZilla to access feature files.
Open your FTP tool and enter the server account information to connect to your site. To find the correct file, navigate to wp-content/themes/[the name of your theme]. When you open this folder, you will see the functions.php file.
All you have to do now is edit it with your favorite text editing software. After making changes, save and overwrite the function file with the exact same name and extension.
Some small cases of using Word Press function files
You should now be able to start editing your functions file. To get you started, here are some examples of the various tweaks you can make. All you need to do is copy the provided code snippets and paste them on a new line at the very bottom of the function file (don’t forget to save it!).
1.Add Google Analytics to your website
There are various ways to integrate Google Analytics with your Word Press website. One of them is to add your credentials directly to the function file. This inserts analytics tracking into the header of your site, ensuring each visit is properly captured.
Start by pasting the following code at the bottom of the function file:
<?php
add_action('wp_head', 'wpb_add_googleanalytics');
function wpb_add_googleanalytics() { ?>
// Replace this line with your Google Analytics Tracking ID
<?php } ?>
All you need to do now is find your tracking ID and paste it into the line containing the placeholder text. When you save the function file, your site will be connected to your Google Analytics account.
2. Change the default login error message
By default, when someone tries to log into a WordPress website and fails, they see an error message like this:
This is not ideal because the site is giving potential intruders information about why the attempt failed. A safer solution is to change it to a generic message.
You can easily do this by adding the following code to your function file:
function no_wordpress_errors(){
return 'Something went wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );
See the “Something went wrong!” message on the second line? This is the message that will appear the next time an incorrect login attempt occurs:
You can change it to whatever you want as long as you keep the single quote character. Try using a different message and see how it works.
3.Add an estimated reading time for the article
This neat trick enables you to calculate and display the estimated time it will take to read a post. Your visitors can then get an immediate overview of how long the content will be.
To implement this code, you need to make two separate edits. The first is done as usual to the function file, where you need to paste the following code snippet:
However, this only performs calculations. You now need to add the following code wherever you want to display the results:
echo reading_time();
For example, you can add this to the metadata displayed next to each article. Every theme is structured differently, but in the 2017 theme it’s located in template-parts > post > content.php.
Estimated reading time will now appear in the title of each article, along with the date.
4.Remove the Word Press version number
Older versions of Word Press may contain security flaws that malicious hackers and bots can exploit. One way to avoid this risk is to hide which version of Word Press your website uses. This is known as security through obscurity.
Before we continue, it’s important to note that obscurity should not be your only security measure. It’s more like adding an extra bastion to your already secure WordPress bastion.
Hiding your version number simply requires you to add the following very simple code snippet to your function file:
remove_action('wp_head', 'wp_generator');
The version number will now be removed from all areas of your website, including its code and your RSS feed.
5.Automatically update your copyright notice
Updating the year in a copyright statement is one of those little tasks that is easy to forget. One way you can keep up is to use this trick, which automatically generates a copyright date based on the year your first article was published.
Then add the following code wherever you want to display the copyright information:
<?php echo wpb_copyright(); ?>
You will now see a dynamically updated copyright date on your website.
In this example, we are adding the date to the footer.php file so that it will be displayed at the bottom of the page.
6.Add custom menu
Most themes come with predefined navigation menus, but what if you want to create your own and place it anywhere on your website? All you need to do is paste this code into your function file:
You can replace “My Custom Menu” with whatever you want to name your menu. If you go to Appearance > Menus in the admin area, you should see the new option listed.
Most commonly, you will need to place this code in your header.php file.
7.Customize your summary
A summary is a short sample description of your article that can appear on your homepage or in search results instead of the full article content. By default, all excerpts have the same length and link text, but you can change this.
First, let’s change the text of the link that takes you from the summary to the full article. This is usually “read more” or “keep reading”, but you can make whatever you want by pasting the following snippet into your function file:
function new_excerpt_more($more) {
global $post;
return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
Here the link text has been set to Read the full article…:
Then, let’s change the length of the summary. Paste this code into your function file:
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
By default, the standard length is 55 words. In this example, it is set to 20. You can change the numbers to whatever you want.
8.Add a random background to your website
Finally, let’s end with an interesting design trick. This tweak allows you to randomly generate a new background color for your website every time someone visits it. First add the following code to your function file:
This code generates HTML markup for the color, so all you need to do now is make sure it’s applied to the page. For this, you need to find the <body> tag, which should look like this:
<body <?php body_class(); ?>>
This is usually located in the header.php file, but can be elsewhere, depending on your theme. Once you find the correct line, just replace it with the following code:
Save your file and open your website now. You should see it have a new background color.
Reload the page and you’ll see a new color each time.
This is obviously not the right design choice for every website, but for some it’s a neat trick.
Summary
The functions.php file of your Word Press website theme is the perfect place to start learning how to modify your site’s default functions. This is a powerful file, and once you understand how it works, you have a lot of control over it.
<?php
add_action('wp_head', 'wpb_add_googleanalytics');
function wpb_add_googleanalytics() { ?>
// Replace this line with your Google Analytics Tracking ID
<?php } ?>
function new_excerpt_more($more) {
global $post;
return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
这里的链接文本已设置为Read the full article…:
然后,让我们更改摘要的长度。将此代码粘贴到您的函数文件中:
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
【WordPress Hosting / 悦然wordpress建站】I am a WordPress hobby from China, and I like to communicate with friends from all over the world to learn WordPress website building and maintenance related knowledge.