0

I'm trying to overwrite following code in the child theme. I want to load file /inc/soundtheme-admin.php from child folder. I copied same code snippet in child-theme/functions.php and also copied the inc/soundtheme-admin.php. I also replaced the get_template_directory() to get_stylesheet_directory() but its still loading parent file.

Please correct me where I'm making a mistake. Thanks in advance.

// Code snippet from parent-theme/functions.php

if( function_exists('acf_add_options_page') ) {

    require get_template_directory() . '/inc/soundtheme-admin.php';

    $paper_themes = 
    acf_add_options_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Theme',
        'menu_slug'     => 'theme-general-settings',
        'capability'    => 'edit_posts',
        'redirect'  => false,
        'autoload' => false,
        'icon_url' => 'dashicons-carrot'
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Options',
        'parent_slug'   => 'theme-general-settings',
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Layouts',
        'parent_slug'   => 'theme-general-settings',
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Code',
        'parent_slug'   => 'theme-general-settings',
    ));

    acf_add_options_sub_page(array(
        'page_title'    => '',
        'menu_title'    => 'Sound Supports',
        'parent_slug'   => 'theme-general-settings',
    ));
}
1

The functions in your child theme will be loaded before the functions in the parent theme. This means that if your parent and child themes both have functions called my_function() which do a similar job, the one in the parent theme will load last, meaning it will override the one in the child theme.

~Guide to Functions and Child themes

Further:

Function Priority

If you're not using your own parent theme, or you're using a third party one without pluggable functions, you'll need another method.

When you write functions you can assign them a priority, which tells WordPress when to run them. You do this when adding your function to an action or filter hook. WordPress will then run the functions attached to a given hook in ascending order of priority, so those with higher numbers will run last.

Let's imagine the function in the parent theme isn't pluggable, and looks like this:

<?php
    function parent_function() {
        // Contents for your function here.
    }
    add_action( 'init', 'parent_function' );
?>

This means the function in your child theme would look like this:

    <?php
    function child_function() {
        // Contents for your function here.
    }
    add_action( 'init', 'child_function', 15 );
    ?>

That should get you started...

  • It is not pluggable and does not look like above example. It is just calling the parent_function in if statement i.e. acf_add_options_page and does not have a declaration in parent=theme/functions.php file. Is there any other way to load that? – MKS Sep 15 '17 at 21:34
  • Great! Good job. Child theme overrides are pretty straight forward until you get to functions. In some cases it's better to unset things specified by the parent. But in this case an override was appropriate. Happy coding! – dwcouch Sep 15 '17 at 22:15
0

Above comment definitely helped. The declaration was in the different file for acf_add_options_page function and you need to include that file child-functions.php file too.

I just copied the code inside if block from parent-functions.php file and paste it in child-functions.php file including required files. That worked for me.

Hope it will help you too.

Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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