4

I have a PHP template in my child theme that calls a function ale_filter(), via

        <div class="articles">
            <?php ale_filter($_POST);?>
        </div>

The function is defined in a parent theme file: /aletheme/functions.filter.php. I would like to copy this file to my child theme, make edits to the function, and ensure the PHP template in-question calls this function, and not the parent theme function. How do I do so?

I have already copied the parent theme file to my child theme, using the same file name and relative path (/aletheme/functions/filter.php).


A previous Stack Exchange question linked to a page with three methods. Those three methods were:

  • Taking advantage of pluggable functions - This method applies only if the parent theme function is pluggable. Mine is not.
  • Increase priority for child theme function - I'm not entirely sure how to do so in my case. The example given was for a parent theme function attached to a WordPress hook, and mine is not. The PHP man page on proc_nice wasn't that clear either. I guess I could follow-up on that a bit though. Maybe someone here has ideas.
  • Making use of hooks, actions and filters - This method applies only if the parent theme function is attached to a WordPress hook. I don't believe that applies in my case.

I found several other Stack Exchange questions, but the answers in all described methods that I do not think apply in my case:

Thanks.

  • Just copy the function and give it a different name, then use that function in your template instead. – Jacob Peattie Apr 11 '18 at 11:46
  • @JacobPeattie OK thanks. But copy the function to where--to my child functions.php? – cag8f Apr 11 '18 at 11:48
  • Yes, into your child themes functions file, or any file included in your child theme’s functions file. – Jacob Peattie Apr 11 '18 at 11:50
6

Since the function is used in your child theme's template there's nothing stopping you from using a function with a different name. So you don't actually need to plug the function, you just need to create a function that does what you want and use that instead.

So copy ale_filter() to your child theme, rename it something like ale_child_filter(), change it however you like, and then just change your template to use that function.

You only need to plug a function if the function is used somewhere that you can't overwrite with your child theme. Plugging it would allow you redefine a function when you don't have access to change where the function is used. Since you do have the ability to change which function is used, plugging the function is unnecessary.

  • OK thanks for that explanation. It worked as you described. – cag8f Apr 11 '18 at 15:30
  • Actually, so I learn: when I copy a PHP template from parent to child theme, the child theme file typically overrides the parent theme file. But in this case, why didn't the same occur when I copied this particular file (filter.php) from parent theme to child theme? When i did so, it did not supersede its parent theme equivalent. Is there something special about template files? Or am I misunderstanding something? One difference I can see is that this particular file isn't a template file--all it contains is one particular function: ale_filter() – cag8f Apr 12 '18 at 7:22
  • By default, only files in the template hierarchy will be replaced with the child theme version if it exists. Any other files or includes need to be built with explicit support child themes by the developer. See my answer to this question for more. – Jacob Peattie Apr 12 '18 at 7:25
  • Great thanks, that's exactly the info I was looking for. – cag8f Apr 12 '18 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.