Support » Developing with WordPress » Conflict with Action and Filter Hooks

  • buffercode



    I have created the same name for action and filter hooks like,

    apply_filters( 'fed_add_main_sub_menu', $menu );
    do_action( 'fed_add_main_sub_menu' );

    the $menu variable will get reset to empty string on add_action on the filter, like

    add_filter( 'fed_add_main_sub_menu', array( $this, 'fed_cp_add_taxonomies_menu' ) );

    So I got the $menu array is undefined. When I changed the do_action name to different like,

    do_action( 'fed_add_main_sub_menu_action' );

    Then the $menu array is working properly.

    Is this normal? Should we assign the different name for filter and action hooks?

    I used this code in Frontend Dashboard Plugin

Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    Yes and yes! Filters and actions are actually the same data type, used in slightly different ways. When you use the same name, WP will call all the callbacks for either one, as they are all saved in the same global array by tag name. In some cases, especially actions, it might not matter as return values are ignored. In other cases bad things can happen. Don’t do that 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Conflict with Action and Filter Hooks’ is closed to new replies.