3

In the parent theme, the following is at the bottom of the functions.php file.

require_once(TEMPLATEPATH . '/admin/admin-menu.php');

In the child theme's function.php, this code will include the child admin panel.

require_once(STYLESHEETPATH . '/admin/admin-menu.php');

As you can see, I shouldn't use both files b/c the bottom file includes get_stylesheet_directory_uri() instead of get_template_directory_uri() for certain localized files (js, css). Thus, I need to remove the parent file from loading

I believe I need to use the remove_action hook, but I'm not sure how to do this right. Can't find a good answer on Google either.

I started writing the following in the functions.php file in my child theme, but I don't know how to write it properly.

function remove_parent_admin_panel {
   remove_action('remove_panel', '[WHAT-GOES-HERE?]');
}

Then I guess I need to use a add_action hook to add the above function to remove the parent admin panel.

Should I wrap the parent require_once with a function statement? Am I on the right track?

2 Answers 2

5

For cases where you want to require/include PHP files, but still allow child themes to replace those PHP files outright, then you should use the locate_template function.

Example: Parent does this:

locate_template( 'admin/file.php', true );

This finds the admin/file.php file in either the child or the parent theme, then does a require on it (that's what the true is for).

So to replace the file in the child, you just replace the file in the child. Simple. Easy.

Note: The method defaults to using require_once. If you just want to require only, then pass a third parameter of false.

5
  • Interesting, that's good to know. So, what you're saying is that, in both child and parent functions.php files, I should include the code you wrote above?
    – micah
    Jul 7, 2011 at 18:27
  • No, in the child theme you don't have to include anything but the modified file. The parent will make the locate_template call, and that call will automatically load the admin/file.php from the child instead of the parent, if the child has such a file. This is the basis for child theme file replacements, the parent makes the call, and the child can then replace specific files without any special coding at all.
    – Otto
    Jul 7, 2011 at 18:28
  • Wow, that's awesome. In all the reading I did about child themes, I never saw this but it's very useful and seems to work correctly for me. Thanks!
    – micah
    Jul 7, 2011 at 18:35
  • @Otto is there a function I can use within my child themes functions file so that I don't modify my parent themes functions file? My parent theme also has require_once( get_template_directory() . '/function-includes/theme-functions.php' ); and replacing it with locate_template works but if my client updates their theme it will overwrite this small mod. Is there any alternative?
    – Anagio
    Feb 16, 2013 at 9:57
  • If the parent is written incorrectly, no amount of fiddling with the child will necessarily be able to fix it. Fix the parent, get the parent theme to have the fix in it.
    – Otto
    Feb 16, 2013 at 10:20
0

Inside of the Parent Theme's \admin\admin-menu.php file, look for the function add_menu_page(). It should be wrapped in a function, and that function called in an add_action( 'admin_init', 'function-name' ) call.

You need to call remove_action( 'admin_init', 'function-name' ) for whatever that function name is.

Your Answer

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

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