Sometimes, we will encounter a situation where our WordPress website theme template has been finalized, but we want to add some new functions to the WordPress website later. What should we do at this time? Continue to modify the WordPress website theme template? Of course, you can certainly modify the theme template. But after a long time, I may be unfamiliar with the current theme template, and it may be difficult to modify it. Is there any other way to add new features to the wordpress website? Of course there are. For some function applications, we can use the form of wp_head() function+plug-in to complete some functions. Let's take a look at how the wp_head() function converts the plug-in data into the foreground page.
1、 Create plug-ins for the wordpress website.
Enter the plugin directory/wp content/plugins/on the wordpress website, and create an app directory under the plugin directory, that is, create a plugin named app. As shown below:
Then, create two files in the app plug-in directory, a bbb.css file and an index.php file, as shown below:
The bbb.css file here is the style file of the plug-in, and index.php is the home page file of the app plug-in. The function we want to implement here is just a demonstration function, which is to modify the font size and color of the foreground page.
The index.php file code is as follows:
<? Php/* plugin name: app//This is required, otherwise the plugin uri cannot be found in the background plugin list: //wanlimm.comauthor: Non-stop version: 1.0description: This is a plug-in that uses the wp head() function */function my_head() {//Add a style file echo<link rel="stylesheet" href=". WP_PLUGIN_URL./app/bbb. css">;} if (! Is_admin()) {//If it is not the background, add the my_head function to the action hook wp_head hook of wordpress. add_action ("wp_head" ,"my_head");}
The code of the bbb.css file is as follows:
body{color:red; font-size:40px;}
Change the text color of the front page of the wordpress website to red, and the font size to 40 pixels.
2、 Enable plug-ins in the WordPress website background plug-in list.
After adding in the first step, we can see the app plug-in in the background plug-in list of the wordpress website, as shown below:
We click the "Enable" button to enable the app plug-in. Only when the plug-in is enabled will its functions be applied to the front page of the wordpress website. Of course, although we have enabled it now, the font of the frontend of our WordPress website is still unchanged. The following figure (the content we added last lesson).
3、 Add calling code in the WordPress website theme foreground.
Through the above two steps, we have prepared the function and are waiting for our WordPress website to call. At this time, we need to use the wp_head() header function, which is very powerful. It can add all the action functions we added to the wp_head hook of WordPress to the front page of the WordPress website. However, this wp_head() function must be added to the<head>tag on the front page of the wordpress website to be effective, as shown below:
At this time, let's take a look at the front page of the WordPress website to see if the page font has changed. At this time, the font becomes larger and red. As shown below:
The "Back to Home" beside it is blue because it is a link. If you want to change its color, you must set its color separately. Let's take a look at the source code again, as shown in the following figure, the wp_head() function inserts the style file bbb.css of the plug-in app we created into the<head>tag on the foreground of the wordpress website.
Of course, the wp_head() function will insert many other codes into the<head>tag at the same time as the plug-in bbb.css style file. Some codes are useless to us, and we can deal with them by some means. This will be introduced in our later articles.
All right, so far, this section is drawing to a close. The purpose of this lesson is to introduce“ How to add data code to the header of the front page of the wordpress website through the wp_head() function ”This function is widely used. Many wordpress plug-ins use the wp_head() function. If this function is not used, these plug-ins cannot be used.
These are my views. If you have different views, please comment. At the same time, welcome [like, share, collect] and [follow] me.