最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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' );
发布评论

评论列表(0)

  1. 暂无评论