If you look at the word press page with available developer tools, you may think it is mainly composed of HTML and CSS, and occasionally JavaScript. But what we see there is only the final product. In the background, most of a given WordPress page actually consists of PHP functions and database queries. They export pages in a form that browsers can parse and view. CSS is good for creating surface changes (color, size, etc.), but to make larger, more structured changes, you must work with the source code. One possibility to do this is to find the appropriate line of code in the core file and replace it with your own. It (obviously) works and is a method that some people continue to use. The
However, the disadvantage of this method is that all customizations will disappear in the next word press update. Overwrite core file – dung – all changes will disappear. In addition, not updating will bring serious security risks, so this is not a good idea. So what do we do? Fortunately, there is a way to change the code in word press without actually changing it. Seems impossible? Let me introduce WordPress Huck. What is a WordPress hook? Hooking is a very powerful tool. Knowing how to use them is one of the most important concepts in plug-in and theme development. The
For example, hooking is widely used in theme frameworks such as geneis to allow easier theme customization. But don’t be afraid. Even if you are not a developer, but are looking for a more elegant and efficient way to change your site, you can still benefit from learning WordPress hook. So what are they? First, it is easy to find the hook. Whenever you see code fragments wrapped in the do\u action() or apply\u filters() functions, you will find your own hook. WordPress core, plug-ins, and themes. The
For example, in the title section of many topics, you can find lines of code similar to the following: By investigating the [php] male [\/php]wordpress core file to determine what is behind wp\u head, you can find the following: [php] function wp\u head() {do\u action (\
The code does not actually perform any function, but uses wp\u head as an anchor. That is, after WordPress processes the wp\u head function, other functions and code to be executed will be wp\u head. As a result, custom code does not have to be physically placed in the same location, but is imported externally. This allows you to run unique functionality in the core configuration without changing the code in the core file. It’s sweet, huh? The best thing is that no matter how long WordPress is updated, the changes will remain unchanged and will not be overwritten. The
Be sure. Shall we go a little deeper? Main tools: tasks and filters WordPress hook relies on two main methods: tasks and filters to change functionality. The corresponding functions of WordPress are add\u action() and add\u filter(). The term hitch is often a comprehensive term for these functions and is also used for placeholders that can be connected to a hitch. To make the problem more confusing, you sometimes hear action hooks and filter hooks. But don’t worry too much about it. It is more important to understand the methodology. The
Actions and effects
In general, the syntax is very similar, but the operation is very different. When you connect a task to a placeholder, the task runs with all the features that already exist in that particular location. Therefore, the action hook is used to add items to the site. For example, to create a new part area in the title or footnote of a web site, you can use the task hook. Instead, the filter modifies existing functionality. They are located between the database and the browser, and can adjust the code passed in any direction. An example of using a filter hook is if you want to replace the \
The main difference between a task and a filter is that the latter must return parameters back to the original function. That is, get the arguments from the code to be filtered, change them in any way, and then provide the adjusted parameters back to the previous project. Instead, the task simply adds some extra code to the connected functionality and is usually not interested in other existing tasks. Do you understand. Don’t worry if the details are still vague. When you get to the actual code, everything becomes clearer. How do I use attached structural actions and filters? Typical structures include: The
[php]add_action (\
Note: the hitch name is also called a tag. 2. your function name is followed by the name of the function to be connected to the tag. It can be its own features, standard PHP features, or features in the core of word press. If you decide to write it yourself, remember the following: First, make sure that the name consists only of letters, numbers, and underscores. Do not use hyphens to distinguish words. This violates the word press encoding standard. Second, note that PHP does not allow multiple functions with the same name. Therefore, it is important to choose a unique name to avoid conflicts. The
A common way to do this is to prefix the name with an initial or other unusual identifier (such as nick_awesome\u function). In addition, you must ensure that the function name is understandable and meaningful. Although you can choose any name, it is much easier for others to build task-based code if the code is self documenting. 3. priority the priority factor determines the order in which the code runs when multiple functions are connected to the same placeholder. The priority is determined by a number between 1 and 999, and the default value is 10. The lower the number, the faster the function will run. This is often not important, but in some cases, priority must be assigned. Please note that this part may be covered by the filter hook. It is also important that priority takes precedence over the functional order of the file. If one is at the beginning and the other is at the end, and the latter has higher priority, WordPress will run first. 4.accepted_parameters this statement provides the number of parameters passed by the function. There is only one default setting. Defining allowed parameters is usually optional. However, if the filter hook passes multiple arguments, it is usually required. In this case
The number of variables must be clearly defined. When you’re done, just create the function you want to run. Does it look simple? Very good. Theory is enough. Let’s look at some concrete examples. Yes, in the next use case, we used the development site running on the twenty fifth topic. In case of curiosity about the name of the hook. First, I want to change the footnote. Male topic footer. When viewing the PHP file, you have the following code: [php]do\u action (‘twentyfifteen\u credits’); [\/php] if you follow me, you will find that this is the fatal signal of the action hook. In this case, I would like to add a personal greeting to the footnote of my website. You can: [php]add\u action (‘twentyfifteen\u credits’,’nick\u footer\u greeting’); Function nick\footer\u greeting() {echo \