theme development - Where i must put hooks in overridable functions?

Where i must put hooks in overridable functions for better child themes? Inside if statement or outside?if( !function_ex

Where i must put hooks in overridable functions for better child themes? Inside if statement or outside?

if( !function_exists( ovveridable_function() ) {
    function overridable_function() {
        echo 'Test';
    }
    add_action( 'init', 'overridable_function' );
}

OR

if( !function_exists( ovveridable_function() ) {
    function overridable_function() {
        echo 'Test';
    }
}
add_action( 'init', 'overridable_function' );

Where i must put hooks in overridable functions for better child themes? Inside if statement or outside?

if( !function_exists( ovveridable_function() ) {
    function overridable_function() {
        echo 'Test';
    }
    add_action( 'init', 'overridable_function' );
}

OR

if( !function_exists( ovveridable_function() ) {
    function overridable_function() {
        echo 'Test';
    }
}
add_action( 'init', 'overridable_function' );
Share Improve this question asked Jun 29, 2019 at 8:22 wpdevwpdev 5492 gold badges13 silver badges28 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 2

Neither. Hooked functions don’t need to be pluggable because child themes can already unhook and replace them with remove_action().

The main functions that you’d want to make pluggable are functions that are used in templates i.e. template tags, and those functions aren’t usually hooked, so the placement of add_action() isn’t relevant. Even then you probably only need to make them pluggable if they’re used in multiple templates, because otherwise the child theme could just replace the template file.

Try with outside if

function overridable_function() {
    echo 'Test';
}
add_action( 'init', 'overridable_function' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745359424a4624291.html

相关推荐

  • theme development - Where i must put hooks in overridable functions?

    Where i must put hooks in overridable functions for better child themes? Inside if statement or outside?if( !function_ex

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信