Community Forums › Forums › Archived Forums › Design Tips and Tricks › How can I create a "child" functions.php?
Tagged: child theme, php
- This topic has 5 replies, 4 voices, and was last updated 11 years, 5 months ago by
Brad Dalton.
-
AuthorPosts
-
September 29, 2013 at 2:40 pm #64695
KeithAdv
MemberBecause I'm not the greatest coder, I really benefit from safe-coding practices so I don't break too many things! (As well as protect my modifications from being overwritten either by Genesis or Child Theme updates.)
So, even though I'm technically using a child theme with Genesis already, I want to be able to make a "child theme of a child theme." I've already learned out to do that with CSS:
Assume my Genesis child theme is called MyTheme. I created a MyTheme-Child directory (named after MyTheme) and put a mytheme-custom.css file in it.
Then I put this code (which I learned about here) in MyTheme's functions.php file:
add_action( 'wp_enqueue_scripts', 'maj_load_custom_css' );
function maj_load_custom_css() {
wp_enqueue_style(
'custom-css',
get_theme_root_uri() . '/MyTheme-child/MyTheme-custom.css',
array(),
PARENT_THEME_VERSION
);
}
Excellent. That gave me the training wheels I needed for MyTheme CSS modifications.
Now I'd like to do the same thing for my MyTheme's functions.php. (In fact, the above code is something I'd like to put in that child file.) I've also been putting some other custom code in that file and I'd like to protect it from overwrites.
I'm guessing that it's just one line I need to add to MyTheme/functions.php, but so far my attempts are getting error messages like "undefined function in line...". It's finding the file, but I'm not calling it right or calling it at the right time, etc.
I'd appreciate a push in the right direction! Thanks!
September 29, 2013 at 4:46 pm #64705Brad Dalton
ParticipantSeptember 29, 2013 at 8:09 pm #64732dev
ParticipantFrom what I've known about Genesis, creating a child theme of a child theme is just not done. It is easy enough to create and import a custom.css file, but I can't think of any really good reason to create a custom function.php file. Most people don't have much code in a functions.php file that they could not easily copy and paste to a new functions.php file should the child theme be updated.
Just put your code at the tend of the function.php file that your theme already has. If it doesn't work... you'll know in a hurry.
But if you really want to have your own code file, you could put an 'include' statement at the end of the functions.php file in your theme. See http://php.net/manual/en/function.include.php. I've never seen this done in any theme, but it should work... assuming you now how to code an PHP file.
September 30, 2013 at 12:23 am #64746KeithAdv
MemberThanks, Dev.
I appreciate the advice!
I'm probably being overly careful. Still, I would argue that the "child theme of a child theme" makes sense in the Genesis case, since the child theme is the one we're operating on. I'd argue that whatever logic makes it a good idea in the rest of the WordPress world, holds up here. The child theme I've been using (Ayoshop) has updated itself on occasion, and it would be annoying if my code got overwritten in the process. My custom CSS is safely off in another directory while the original style.css is pristine.
However, the second problem may not be as easily solved. What I think I've learned up to this point is that WP is going to load and try to execute that separate, child-of-a-child functions.php first, before the parent functions.php. Because of that, It doesn't know how to execute any functions it finds there and that's why it aborts. Therefore, even though the include statement works, it doesn't!
Since this is a nice-to-have and not something I'm desperate for, I think I'll just keep putting whatever functions I need in the parent file for now and make sure it's backed up so i don't get caught by an update.
November 11, 2013 at 4:02 am #72274croosen
MemberHi there,
I was wondering the same. As my clients have full access to their websites, they might want to update the child theme (by accident or not) and then all my custom mods in functions.php are gone.
I have found that I can include a custom functions.php in the child themes' functions.php this way:
- in your child themes' folder create a file called "custom-functions.php". This can be an empty file at first, where you can put your custom code.
- open your child themes' functions.php and include this file right at the top after the"require_once" like this:
//* Start the engine require_once( get_template_directory() . '/lib/init.php' ); include_once( 'custom-functions.php' );
Now in your custom-functions.php you can add your custom functions code, I tried to insert a piece of code to simply change the footer credits (see more snippets to try here http://gregrickaby.com/genesis-code-snippets/), and this worked.
This way I think, if the theme gets an update, the "custom-functions.php" might stay intact and you can easily include the files again.
BUT THIS IS NOT TESTED AFTER AN UPDATE. I did not test if an update replaces the whole child-theme folder and lets the custom files stay or not. You might want to try to include from a different dir to be safe, if someone has an example of that please let me know.
November 11, 2013 at 4:21 am #72275Brad Dalton
ParticipantNormally they use a different file name and theme name so it installs as a different child theme.
Child themes are not auto updates so it can't over write the existing files.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.