A Guide to Overriding Parent Theme Functions in Your Child Theme

thumbnail


If you have any experience working with parent and child themes in WordPress, you will know how to add template files in your Children theme to your parent theme. So for example if your parent theme has a page pfp file and you create a new one in your child theme, use it in the child theme when displaying WordPress pages.

You think the functions will work like this: Functions of your child theme Create a new function in the PFP file that has the same name in the parent theme, and it will take precedence. Unfortunately, this is not easy.

In this tutorial I will show you three ways you can remove functions from the parent theme in your child’s theme.

  • Plugable functions
  • Event priority
  • Remove the functions from the hook to which they are attached

How functions work in parent and child themes

Before examining the ways in which functions work in their children’s topics, it helps to understand how functions work in parent and children’s topics.

First of all, you need to know that your parent will run all the functions in the theme when you are using the child theme. You do not need to add anything to your child theme’s function file to do this. This is different from CSS, where you have to manually add a parent theme style sheet to the children’s theme style sheet.

Another thing to know is that if your parent and child themes have functions with the same name, it will break your site – unless the parent theme has a function plug-in function ( More on that soon). That is why it is important to use the former for all your actions, and to use a different term in parent and child topics, only then.

However, you can change the order in which the functions are dismissed, and you can prevent the functions from being dismissed completely, as we will see soon.

Capable goods

Good stuff is something you encode in your parent theme, so if you are not working with an existing parent theme they will not be of any use to you.

But if you’re writing your parent theme, perhaps as a starting point for future plans, or if you’re developing your own theme framework, it’s a good idea to make your actions plug-enable so you can Easily subdivide into child themes. It’s also a good idea to look at the functions written on the parent theme, as many of them will include plug-in functions, including the WordPress default theme.

To write a plugable function, you just lock it in a conditional tag to check if a function with that name already exists:

So if you attach your parent theme to such conditional tags, WordPress will check if your child theme has a function with the same name that is already running, and if In that case, this function will not work. From the parent theme.

Then when you come to write a function in your children’s theme that you want to override one of the parent themes, you just say the name that is included in the parent theme.

WordPress will run the function in the child theme first, and when it comes to one of the parent themes, it will check if it already exists and why it does, it will not work. ۔

Work priority

If you are not using your parent theme, or you are using a third party without plug-in functions, you will need a second method.

When you write functions, you can assign them a priority, which tells WordPress when to run them. You do this when you add your function to a process or filter hook. WordPress will then run the functions associated with a given hook in ascending order of priority, so more numbers will run in the final.

Let’s assume that the function in the parent theme is not feasible, and so it seems.

This function is attached to the hook and is not preferred. Default WordPress assignment is assigned 10 Add to functions that are not prioritized, so dismiss your function after you use a larger number 10. I have to use 15 So if I want to add another function between the two later, I have found some breathing room.

This means that the function in your child theme will look like this:

Alternatively, this may take precedence over the event in your parent theme.

So all you have to do is make sure that the priority you give to your children’s theme is high.

Remove functions from hooks

Sometimes after running another function it is no more than before – you have to make sure that the function in the parent theme does not work at all. In this case, you can attach the parent theme function to the hook attached to it. remove_action() Or remove_filter() Actions. Depending on which one you use, the function is linked to an action hook or filter hook in the parent theme.

So let’s go back to our previous function in the parent theme:

To remove this function from the action hook and therefore prevent it from firing, you create a function in your child theme to remove it using remove_action():

However, it will not work by itself – you need to attach this function to a hook that will fire after the The hook to which the parent theme function is attached. This is because you cannot terminate the action before dismissing. You can find the details of the order in the codex.

Once you’re done, you can simply write an alternate function to replace the parent theme function in your child’s theme functions file, or remove the functionality from the parent theme if you want to do everything happens.

A note on preferences

Note that if you are trying to remove a function using remove_action() Or remove_filter() And yours, this event is a priority compulsory Add priority when removing it, or it won’t work.

So if the function in the parent theme looks like this:

… will need to add the same preferential value when you delete it.

Advanced Parent and Kids Theme Functions: Plugable Functions and Extended Functions

In addition to the above, there are certain scenarios in which you want to move things forward. The following points are in response to the questions asked in the comments below.

How to partially ignore a function in the parent theme

Strictly speaking, you cannot partially override a function. You can allow it to run or disable it. But there are limits to the work.

The first is that if you want to partially subdue a function, it includes functions that are plugable. You may find that this function includes a call to another plugable function from within the parent theme.

If this is the case, you can override any function inside the main function, if this is the code you want to change. Do this by writing a new version of the plugable function that contains either one return Contains a statement or alternative code. Note that this will only work if the function is plugged in – if not, your site will crash.

However, this method only works if you are lucky enough to need to subtract functionality within a function provided by another existing and plug-in function.

In most cases, this will not be an option. In that case, you’ll need to disable the function and replace it with a different name, with a function in your name. To create your new function, you can copy and paste the function from parent theme to child themes. Functions. PPP File, and then edit it so you don’t want the code.

You will use two of your functions: one to remove the original, and the other to provide a new code, for example to subdue a parent theme based on indie hook:

How to subdue incomparable functions

Functions in the parent theme that are not plug-in cannot be subdivided in any particular way. So you have to use the above method, remove the function from its action hook and then write a new function on the same action hook that provides the new code.

Don’t name the function in your child theme the same as the parent theme because it will ruin your site!

How to enhance parent theme functions

Functions are not usually designed to be scalable. However, this may be an exception if your function includes calls to other plug-in functions.

If this is the case, you can extend this function by overriding the functions in the main function, without changing all the functions in the main function.

In most cases, you can enhance the parent theme function in just one of three ways:

  • If the function you want to extend is feasible, copy it to your children’s theme and add additional code to extend it.
  • If the function is not plugged in, unhook it using it remove_action() And write a new function (with different names) on the same hook with additional code. You may want to copy the code from the original function to your new function and edit it.
  • Write a second function that is called on the same hook as the first function and adds additional code that runs before or after the first function. I use the priority parameter add_action() Specify whether your second function will run before or after the original function.

Abstract

The overriding function in the parent theme is more complex than overriding the template files or styling, but it can be done. Here are three ways to do this.

  • If you are writing your parent theme, or using one of these, use plug-in functions so that the child theme has the same name as the parent theme.
  • Prioritize functions in your children’s topics to make sure they are in line with your parents’ theme.
  • Use remove_action() Or remove_filter() To completely remove the functions in the main theme.

Which method you will use will depend on whether your parent theme code is coded and whether you need to remove the parent theme function completely or not And the function needs to be run.



Source link

Leave a Reply

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

Back To Top