0

So I've tried a few different ways to override a method inside the parent theme and I'm not having much luck at all.

So here is the structure:

themes/
- wp-starter
-- custom_header.php
- wp-starter-child
-- custom_header.php

I have a method inside the parent custom_header.php as shown below:

function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'wp_bootstrap_starter_custom_header_args', array(
        'default-image'          => '',
        'default-text-color'     => 'fff',
        'width'                  => 1000,
        'height'                 => 250,
        'flex-height'            => true,
        'wp-head-callback'       => 'wp_bootstrap_starter_header_style',
    ) ) );
}
add_action( 'after_setup_theme', 'wp_bootstrap_starter_custom_header_setup' );

Now.. I want to be able to call that method inside my child custom_header.php and override the width and height.

Here are a few attempts:

Added priority to action (Didn't work):

function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'wp_bootstrap_starter_custom_header_args', array(
        'default-image'          => '',
        'default-text-color'     => 'fff',
        'width'                  => 1000,
        'height'                 => 500,
        'flex-height'            => true,
        'wp-head-callback'       => 'wp_bootstrap_starter_header_style',
    ) ) );
}
add_action('after_setup_theme', 'wp_bootstrap_starter_custom_header_setup', 20);

Renamed the method and added priority (Didn't work):

function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'wp_bootstrap_starter_custom_header_args', array(
        'default-image'          => '',
        'default-text-color'     => 'fff',
        'width'                  => 1000,
        'height'                 => 500,
        'flex-height'            => true,
        'wp-head-callback'       => 'wp_bootstrap_starter_header_style',
    ) ) );
}
add_action('after_setup_theme', 'wp_bootstrap_starter_custom_header_setup', 20);

Added an init action call with priority (Didn't work):

function wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'wp_bootstrap_starter_custom_header_args', array(
        'default-image'          => '',
        'default-text-color'     => 'fff',
        'width'                  => 1000,
        'height'                 => 500,
        'flex-height'            => true,
        'wp-head-callback'       => 'wp_bootstrap_starter_header_style',
    ) ) );
}
add_action('after_setup_theme', 'wp_bootstrap_starter_custom_header_setup' );
add_action('init', 'wp_bootstrap_starter_custom_header_setup', 15);

So I've tried doing remove_action('after_setup_theme', 'wp_bootstrap_starter_custom_header_setup'); with no results.

  • Note that a method is specifically a class function, but I see no classes in your code, you should edit your questions title and text so that it doesn't confuse people – Tom J Nowell Apr 3 '20 at 14:31
  • @TomJNowell, ahh I didn't know that! I call things methods outside a class also - I'll make sure to edit it. How would I override a function? – DevSem Apr 3 '20 at 21:33
  • the function is using apply_filters() so you could use add_filter( 'wp_bootstrap_starter_custom_header_args', 'your_function' ) etc to set new parameters for width and height – Michael Apr 4 '20 at 1:58
2

You can't just redeclare the function in the child theme or call add_action a second time. It doesn't replace it, it adds a second hook. As a result, you haven't overriden it, you've duplicated the original. Child theme overrides only work for templates.

What's more, by adding a second definition of wp_bootstrap_starter_custom_header_setup you've declared the function twice, which would generate a PHP fatal error. You can't have 2 functions with the same name.

So first, we need to rename your function so that there's valid PHP:

function sams_custom_header_setup() {

Next, add_action adds an action. It has no concept of replacing an action. If you call add_action 5 times, the function will run 5 times.

So lets add our action for the new setup:

add_action('after_setup_theme', 'sams_custom_header_setup' );

But remember, the original function got added too, so now both will run! So, remove the original:

remove_action( 'after_setup_theme', 'wp_bootstrap_starter_custom_header_setup' );

TLDR:

  • You can't "override" actions.
  • But you can remove them and add a new action to replace them.
  • Stop making multiple functions with the same name! That's invalid PHP, it'll break things
  • Child themes let you override templates loaded via WP, not arbitrary PHP files, functions, hooks, etc

Edit:

It seems the function in your parent theme uses apply_filters, you could just filter the parameters on the wp_bootstrap_starter_custom_header_args filter and modify the array itself. You don't need to mess around replacing the function, or calling add_theme_support

1

Lower numbers have higher priority. Default priority is 10, so if you want an action to be executed before, try with 9.

Also, you should remove the original actions to prevent any unwanted side effects.

function override_wp_bootstrap_starter_custom_header_setup() {
    remove_action('after_setup_theme', 'wp_bootstrap_starter_custom_header_setup', 10);

    add_theme_support( 'custom-header', apply_filters( 'wp_bootstrap_starter_custom_header_args', array(
        'default-image'          => '',
        'default-text-color'     => 'fff',
        'width'                  => 1000,
        'height'                 => 500,
        'flex-height'            => true,
        'wp-head-callback'       => 'wp_bootstrap_starter_header_style',
    ) ) );
}

add_action('after_setup_theme', 'override_wp_bootstrap_starter_custom_header_setup', 9);

0

I am customizing the same theme. In my child theme functions file, I did the following to override the height:

/*
 * Customize the header image parameters
 * See parent theme /inc/custom-header.php for reference
 */
function custom_parameters_wp_bootstrap_starter_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'wp_bootstrap_starter_custom_header_args', array(
        'height'                 => 500
    ) ) );
}
add_action( 'after_setup_theme', 'custom_parameters_wp_bootstrap_starter_custom_header_setup', 9 );

The trick was to add a lower priority, which make the changes execute after the original definitions (default priority is 10).

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.