How can I create a "child" functions.php?

Community Forums Forums Design Tips and Tricks How can I create a "child" functions.php?

This topic is: not resolved

Tagged: ,

This topic contains 5 replies, has 4 voices, and was last updated by  braddalton 1 year ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #64695

    KeithAdv
    Participant
    Post count: 18

    Because 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!

    • This topic was modified 1 year, 1 month ago by  KeithAdv.
    • This topic was modified 1 year, 1 month ago by  KeithAdv.
    #64705

    braddalton
    Participant
    Post count: 10593
    #64732

    dev
    Participant
    Post count: 468

    From 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.

    Dev

    http://www.NewMediaWebsiteDesign.com

    #64746

    KeithAdv
    Participant
    Post count: 18

    Thanks, 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.

    #72274

    croosen
    Member
    Post count: 1

    Hi 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.

    #72275

    braddalton
    Participant
    Post count: 10593

    Normally 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.


Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.