0

In functions.php for a theme I include a file that adds functionality to allow the easy duplication of posts - the include file contains the code from this (very useful) webpage. I include it like this:

include "inc/duplicate-posts.php";

The include contains all the functions from that page, plus the add_filter line that hooks it all up, and it works fine in that theme.

However, I also use this theme in another site as the parent to a child theme. It doesn't add the post duplication functionality here, and I can't see why.

The file is still being included (from the parent theme, via its functions.php), if I add an echo 'lkaslasjd'; line that outputs ok. But the function that's supposed to be connected via post_row_actions doesn't seem to execute, and the extra functionality isn't added.

This is probably a simple misunderstanding on my part, but can anyone help me understand what I need to do to get this to work?

Update: Code works fine, error was all mine

1
  • What's the actual code that's not working? Commented Feb 23, 2023 at 2:21

1 Answer 1

0

There has been a major misunderstanding of child themes. Parent and child themes are a WordPress feature, not a PHP feature, and include is a PHP directive, not a WordPress function.

To make the distinction and reason clearer child themes don't inherit a parents "files" they inherit the parents "templates".

So include "inc/duplicate-posts.php"; will always be relative to the current file that's calling it, not the root of the current theme, and it will never look in the parent theme.

Instead, it looks like there's been a misunderstanding about how templates are meant to be loaded in WordPress.

This loads a template file in your theme:

get_template_part( 'template', 'file' );

It will load the first file in this list that it can find:

  • template-file.php in the child theme
  • template-file.php in the parent theme
  • template.php in the child theme
  • template.php in the parent theme

Always use get_template_part for templates, files that render HTML. These can be overloaded/replaced via child themes.

Never use get_template_part for library files or files that define classes and functions, e.g. your includes folder. Always use include/include_once/require/require_once for arbitrary PHP files that declare hooks/functions/classes, libraries etc.

3
  • Thanks for trying to help, I think I must have explained it badly. In the functions.php file of my parent theme I include the file/code that adds functionality to allow post duplication. This works fine (when I use only the parent theme). However, I have another project that uses the same theme as a parent, plus a child theme. My understanding is that both themes' functions.php should be used, and therefore the duplicate post functionality should still work. Commented Feb 23, 2023 at 9:46
  • can you share the code, as well as the function you're trying to use, and where you're using it, as well as what it's meant to do, and what it actually does instead
    – Tom J Nowell
    Commented Feb 23, 2023 at 9:56
  • Apologies, it all works fine, I'm an idiot - I'd commented out a crucial line and despite looking at it many, many times somehow could not see that?! Thanks again for trying to help Commented Feb 23, 2023 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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