• Resolved loopforever

    (@loopforever)


    Hello,
    There is something I don’t understand about hooks. I would be glad if you help.
    As you know, an action can be defined with do_action. Then it is interfered with with add_action. However, there is a situation like this:

    A do_action definition is made in a plugin I use. However, no action/definition is made with add_action anywhere (in the file etc.). However, if I delete this hook, some things are also deleted. For example, a form. However, there is no form definition anywhere with add_action.
    HOW is this possible?
    I hope it is understood.

    2-) The hook with the same name was used in more than one different file?
    For example,

    in plugins/pluginX/edit.php
    <?php do_action( 'edit_after_display' ); ?>

    or

    in plugins/pluginX/auction.php
    <?php do_action( 'edit_after_display' , $post_id); ?>

    Is this a correct use ? What could be the desired goal?

    • This topic was modified 3 years, 7 months ago by loopforever.
    • This topic was modified 3 years, 7 months ago by loopforever.
    • This topic was modified 3 years, 7 months ago by loopforever.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    A do_action() call simply indicates code execution has reached a certain point. It’s a place where we can add callback functions to do extra things at that point, like output some extra form fields.

    If you remove a do_action() call and lose content as a result, callbacks have certainly been added to that action one way or another. Usually added with add_action(), but due to quirks of the WP hook scheme, add_filter() can also work. Code could even directly add their callback to the global $wp_filter array.

    Most likely there’s a related add_action() call somewhere and you’ve merely failed to locate it. You could try searching for the action name/handle instead of the function name. Just be aware that some action names are dynamic. A made-up example:
    do_action( "edit_{$position}_display" );
    Depending on context, $position might be ‘before’, ‘during’, or ‘after’.

    For similar reasons, multiple calls of do_action() with the same static name can and do happen. Perhaps the author anticipates any callback added in one place would also be needed in another because it’s a parallel situation. For example, if you wanted to add a certain field on one form, you’d presumably want the same field also added to a close relative of the form.

    While it’s not required, I like to always pass the same number of arguments in repeated do_action() calls, even if they end up being empty strings or null. If you truly need a variable number of arguments, put them in an array and consistently pass that array as the do_action() arg.

    • This reply was modified 3 years, 7 months ago by bcworkz.
    Thread Starter loopforever

    (@loopforever)

    Perfect answer. Thank you so much. Yes, I just saw this. Used in theme.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem understanding hooks’ is closed to new replies.