Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I was wondering why isn't anyone (haven't be able to find one with it) adding custom hooks an the beginning and at the end of the theme's function.php for theme development.

The reason I'm asking this is while making a child theme, I had to overwrite some default setups like image sizes set in the parent theme's main function.php (no !function_exists to overwrite on). So anything I write in my child's function.phpwill be overwritten.

Aside from of course adding the function_exists wrapper, the solution I had in mind was just adding a do_action() at the beginning and at the end of the function.php. But is this a bad practice? If so, why?

I am unable to find any answers or anyone asking this anywhere.

share|improve this question
    
Why at the beginning and the end? –  Brad Dalton Feb 12 at 14:05
add comment

1 Answer

up vote 7 down vote accepted

Code in a functions.php (or any file loaded by that file) should not run automatically, eg. when the file is included. All code should be bound to hooks.
That means, you don’t need a special action in the parent theme. The child theme can turn off all parent theme callbacks with remove_action() and remove_filter().

But you can make that easier for child theme authors by adding a custom action after you have registered the callbacks.

Example

Parent theme

namespace ParentTheme;
// register callbacks
add_action( 'after_setup_theme', __NAMESPACE__ . '\setup_custom_background' );
add_action( 'after_setup_theme', __NAMESPACE__ . '\setup_custom_header' );
add_filter( 'body_class',        __NAMESPACE__ . '\enhance_body_class' );

// provide an entry point
do_action( 'parent_theme_loaded', __NAMESPACE__ );

Child theme

add_action( 'parent_theme_loaded', function( $namespace )
{
    remove_action( 'after_setup_theme', $namespace . '\setup_custom_header' );
});

See also this similar example for outdated PHP versions.


If your parent theme ignores the principle of separation of concerns and mixes everything in just one or two setup functions, use a later priority and overwrite those calls:

Parent theme

add_action( 'after_setup_theme', 'some_setup_function' ); // priority = 10

Child theme

add_action( 'after_setup_theme', 'fix_theme_setup', 11 ); // priority = 11

function fix_theme_setup()
{
    remove_theme_support( 'post-formats' );
    // more changes here
}
share|improve this answer
    
Do you have a full functions.php example of your answer? It would help to understand the concept if we could see the whole thing. –  Daniel Feb 6 at 7:35
    
@Daniel What do you miss? –  toscho Feb 6 at 7:37
    
Sorry, the whole concept is blur. How many custom actions do I add? –  Daniel Feb 6 at 7:44
    
Just one after the list of callback registrations. –  toscho Feb 6 at 7:46
    
The problem is using remove_action() would remove a lot of other "init setup" stuff which I want. For example, wenty fourteen has a set_post_thumbnail_size( 672, 372, true ), along with many other setup functions. But because there isn't any function_exist wrapper or any hooks, I can't overwrite that in my child theme. Unless I copy everything in the setup and remove_action() the hook in the parent theme. What I'm suggesting is why not a simple hook which will let me overwrite one function? –  Bobby Feb 12 at 12:43
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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