1

I am trying hard to understand do_action but not understanding. I was studying the blane theme. And they have this code in sidebar.php

<div id="secondary" class="widget-area col-md-3" role="complementary">
        <?php do_action( 'before_sidebar' ); ?>
        <?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>

            <aside id="search" class="widget widget_search">
                <?php get_search_form(); ?>
            </aside>

            <aside id="archives" class="widget">
                <h1 class="widget-title"><?php _e( 'Archives', 'blain' ); ?></h1>
                <ul>
                    <?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
                </ul>
            </aside>

            <aside id="meta" class="widget">
                <h1 class="widget-title"><?php _e( 'Meta', 'blain' ); ?></h1>
                <ul>
                    <?php wp_register(); ?>
                    <li><?php wp_loginout(); ?></li>
                    <?php wp_meta(); ?>
                </ul>
            </aside>            

        <?php endif; // end sidebar widget area ?>

    </div><!-- #secondary --> 

But i don't get what is the second line i.e <?php do_action( 'before_sidebar' ); ?> doing. When i remove this line still it outputs the same result.

Can anybody please explain with some example.

1

1 Answer 1

4

If you hook some functions to the before_sidebar action, they will be executed in your code. Your action is now probably without any function hooked, so it returns nothing.

Example:

<?php
add_action( 'before_sidebar', function() {
  echo 'Try me!';
});
add_action( 'before_sidebar', function() {
  echo 'Yep. ';
}, 1);

// this should output "Yep. Try me!"
<?php do_action( 'before_sidebar' ); ?>

The third argument is a priority. In my example I've set the priority to 1 to the second function and it will be executed at first place.

For more information take a look at WP Codex do_action, and add_action.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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