• Resolved Tim Burkart

    (@bigmoxy)


    I created a child theme for the purpose of customization. One of the first things I want to do is customize a function in the parent theme’s function.php file. The problem as I understand it is that a child theme functions.php only augments the parent theme.

    My question is how can I accomplish this as an override via the child theme?

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @bigmoxy ,

    You can override an entire function if the function in the parent functions.php file is conditionally defined like this:

    if ( ! function_exists( 'some_function' ) ) {
    	function some_function() {
    		
    	}
    }

    You can then define the function with the same name inside the child theme’s functions.php because child theme will execute before parent theme.

    Or, you can override an entire function defined in parent theme if it is assigned as a callback to a hook.

    For example, in your parent functions.php, you have a callback function:

    add_filter( 'the_content', 'append_thank_you' );
    function append_thank_you( $content ) {
    	return $content . '-- thank you!';
    }

    You remove this filter hook in the child theme functions.php as:

    add_action( 'init', function() {
    	remove_filter( 'the_content', 'append_thank_you' );
    } );

    and implement your own custom callback function for the the_content hook.

    You can modify the behaviour of a function in the parent theme if the function itself provides action or filter hooks.

    For example, the function you want to “override” is something like this:

    function some_function() {
    	$title = 'Hello World!';
    
    	do_action( 'some_function_action_hook', $title );
    
    	return apply_filters( 'some_function_filter_hook', $title );
    }

    Then, you can utilise some_function_action_hook action hook and some_function_filter_hook filter hook to modify the behaviour of the function.

    —-

    The purpose of a child theme is to customise parent theme’s features without directly modifying the parent theme’s files.

    Thread Starter Tim Burkart

    (@bigmoxy)

    Hi @kaavyaiyer

    Thank you for your reply. Unfortunately the function I want to override (my_function1) does not fit any of your examples. This is what I have:

    function my_function1() {
        setup google fonts array
     return font_url;
    }
    
    function my_function2() {
        wp_enqueue_style('my_theme-font', my_function1(), array());
        more wp_enqueue_style calls...
    }
    
    add_action('wp_enqueue_scripts', 'my_function2');
    

    I hope this helps.

    Thanks again!

    Hi @bigmoxy,

    Assuming that my_function1() is not being used anywhere else, we can override it as follows:

    As my_function1() is being called inside my_function2(), we will have to first remove/disable the action hook to my_function2()in the child theme’s functions.php like this:

    add_action( 'init', function() {
    	remove_action('wp_enqueue_scripts', 'my_function2');
    } );

    The next step is the define the same 2 functions (my_function1 and my_function2) with different names (my_function3 and my_function4) in your child theme’s functions.php along with your custom implementation and hook it the same way:

    function my_function3() {
        setup google fonts array
     return font_url;
    }
    
    function my_function4() {
        wp_enqueue_style('my_theme-font', my_function3(), array());
        more wp_enqueue_style calls...
    }
    
    add_action('wp_enqueue_scripts', 'my_function4');

    Let me know if this helps!

    Thread Starter Tim Burkart

    (@bigmoxy)

    Hi @kaavyaiyer

    Your last response helped immensely!! I successfully coded the override in my child theme functions file.

    Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to override a parent theme function in child theme?’ is closed to new replies.