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

actions - Understanding and using did_action() in a conditional statement

programmeradmin2浏览0评论

I'm working with Genesis Framework in a custom theme, but this is not a question "genesis-specific".

I need to populate the sidebar of my pages (not posts) conditionally. The condition is to check that it is not already populated by many other related functions (related news, related events, ad banners selected by the user from the backend...).

The actions I need to verify that have not been fired are: genesis_after_sidebar_widget_area and genesis_before_sidebar_widget_area. I'm trying to get it through playing with did_action().

I'm trying with this function:

add_action( 'genesis_after_sidebar_widget_area', 'understanding_did_action' );
function understanding_did_action() {
    if( did_action( 'genesis_after_sidebar_widget_area' ) > 0 || did_action( 'genesis_before_sidebar_widget_area' ) > 0 ) {
        echo '<h2>TESTING</h2>';
        echo 'before: ' . did_action( 'genesis_before_sidebar_widget_area' ) . '<br/>';
        echo 'after: ' . did_action( 'genesis_after_sidebar_widget_area' );
    }
}

and I always get: before: 1 & after: 1, even in the cases that nothing is printed in the sidebar or the action has been fired two or three times.

The point is to find the way to execute the function only when the values are less than "X" (1, 2, 3... it will depend).

Any ideas? Thanks

I'm working with Genesis Framework in a custom theme, but this is not a question "genesis-specific".

I need to populate the sidebar of my pages (not posts) conditionally. The condition is to check that it is not already populated by many other related functions (related news, related events, ad banners selected by the user from the backend...).

The actions I need to verify that have not been fired are: genesis_after_sidebar_widget_area and genesis_before_sidebar_widget_area. I'm trying to get it through playing with did_action().

I'm trying with this function:

add_action( 'genesis_after_sidebar_widget_area', 'understanding_did_action' );
function understanding_did_action() {
    if( did_action( 'genesis_after_sidebar_widget_area' ) > 0 || did_action( 'genesis_before_sidebar_widget_area' ) > 0 ) {
        echo '<h2>TESTING</h2>';
        echo 'before: ' . did_action( 'genesis_before_sidebar_widget_area' ) . '<br/>';
        echo 'after: ' . did_action( 'genesis_after_sidebar_widget_area' );
    }
}

and I always get: before: 1 & after: 1, even in the cases that nothing is printed in the sidebar or the action has been fired two or three times.

The point is to find the way to execute the function only when the values are less than "X" (1, 2, 3... it will depend).

Any ideas? Thanks

Share Improve this question edited Nov 12, 2019 at 10:17 Eje 1654 bronze badges asked May 19, 2016 at 17:07 CapiedgeCapiedge 7881 gold badge7 silver badges17 bronze badges 3
  • 1 I may not be getting your fully, however do not you think that those actions always get called by the Genesis framework? I mean these are hook positions and they are always fired by genesis so that any functions tagged/hooked to those hooks will be called at those locations.. so it will always be 1 i.e. yes action hook was executed. – Muhammad Asad Commented May 20, 2016 at 18:57
  • @Mohsin you put me on the right way. Indeed I was wrong trying to check the genesis hook doing it that way... I got it with "has_action" in spite of "did_action". Many thanks for your time! – Capiedge Commented May 20, 2016 at 19:36
  • Great! I have put the comment in the answer you make mark this question resolved! Good Luck! – Muhammad Asad Commented May 21, 2016 at 11:27
Add a comment  | 

1 Answer 1

Reset to default 0

Those actions always get called by the Genesis framework. I mean these are hook positions and they are always fired by genesis so that any functions tagged/hooked to those hooks will be called at those locations.. so it will always be 1 i.e. yes action hook was executed.

As discovered rightly by OP, using has_action instead of did_action did the job.

发布评论

评论列表(0)

  1. 暂无评论