new functions in child theme

My theme is using a theme-functions.php and I am working with its child theme. I have created a custom menu and addeded it to the code that was inside that theme-functions.php file. This is unsave in case of an update. How exactly can I save my custom menu? I know, there is a way to create a functions.php file for the child and in my case, it would mean that I have to create a theme-functions.php, right? However, I do not see what part of code I would have to put in there because it supposed to load before the original theme-functions.php, right? That would mean, my file would be overwritten. This is confusing. So what is the challenge for me, how can I save my custom menu in case of a theme update?

  • Alexander
    • DEV MAN’s Mascot

    Hello Mark,

    You can create a new functions.php file in the root of your child theme folder (where the theme stylesheet is) In the child functions.php you could place the code to register your menu.
    register_nav_menu( 'primary', 'Primary Menu' );

    Both of the functions.php files will run - the child's and the parent's. Wordpress will also naturally make sure the the child theme's functions.php runs first. Does this make sense? Let me know if you need any more help getting it working or have any other questions!

    Best regards,

  • Mark
    • Flash Drive

    Oh yes, it made sense. Thank you very much. I tried and it worked but I still do not get a snippet to work if I place it in that child functions file. It is a short snippet that excludes some categories from the category widget. That snippet is only working if I place it in the default functions file, as soon as I cut it out and paste it into the childs one, it gets ignored. Here is the code, any ideas?????

    // mm - Exclude categories from widget
    function exclude_widget_categories($args){
    $exclude = "20,21,22"; // The IDs of the excluding categories Tour, HomePage, Intern
    $args["exclude"] = $exclude;
    return $args;
  • Alexander
    • DEV MAN’s Mascot

    Ok, well let's see if we can narrow this down further.

    You can try adding a priority to the filter. It's possible that something else has hooked the filter and is overriding the exclusion key after you set it.

    The 999 tells Wordpress when to run the hook. The default is 10, so we're telling it to come in much much later.
    add_filter("widget_categories_args","exclude_widget_categories", 999);

    See if this changes things. Also, have you tried testing anything else from your child theme's functions.php? Is it possible that the file isn't executing at all?

  • Mark
    • Flash Drive

    Hi there,

    tried, but did not change anything and YES, the functions file, within the child theme, is working.

    Why is this snippet so resistant? I registered a complete new, custom menu in the childs function file and it is working very well.

  • Alexander
    • DEV MAN’s Mascot

    Could you try one more time, instead this time set the priority to 1? Or perhaps increase the value from 999 to 9999?

    Also, are you running this within another function? Or just in the body of functions.php?

    As a general rule, if a piece of code works in one place but not another, then it is a problem of execution order or unreachable code. Because Wordpress hooks basically allow for code to be "saved for later" and run at a specific execution point, you can run into conflicts with multiple plugins or the theme working with the same hook.

    A quick test you could run to debug this would be replacing:

    in place of

    $args["exclude"] = $exclude;

    Then you can find out if something else has already overwritten the values.

  • Mark
    • Flash Drive

    We have to stop this at this point because I think, the functionals file in the child theme is not loaded at all. I thought it is and I tell you why.

    I created a custom template with a custom menu. For that, I have registered the new menu on the default functions file. It all was working fine, I was able to setup the new menu and it appeared on my new template. all fine.

    Then I thought, I have to save this new menu in case of an theme update. I deleted the menu from the default functions file and I added the code the the newly created functions file within the child theme. Then I checked the menu, it was still visible in admin and all my pages were looking ok.

    That made me think, that the functions file within the child theme is working.

    Now I was not so sure about that anymore. For testing, I deleted the menu registration in the child functions file and guess what, the menu was still there, in the admin and on all my pages.

    How can that be. It is not registered anymore. I thought, to register a menu is a must thing to do, no it seems that it is even working without doing it.

    So what is the conclusion? I do not understand how my custom menu can still appear, even if I have deleted all registration for it on all files and secondly.... my child functions file is and never was loading. Why is that?

  • Alexander
    • DEV MAN’s Mascot

    Perhaps the changes to the main file were never saved? Somehow it's being registered from somewhere - maybe another file.

    So if the child theme isn't running, here's what I suggest checking first.

    --File structure.
    Both themes should reside in "wp-content/themes" The child theme should reference the parent from it's style.css and it's functions file should be named the same "functions.php"

    --Theme activation. Are you certain the child theme was active and not just the parent theme?

    Keep me posted on what you find! I hope to help you get this all figured out!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.