Example #1
0
/**
 * Add an item to the main BuddyPress navigation array.
 *
 * @param array|string $args {
 *     Array describing the new nav item.
 *     @type string      $name                    Display name for the nav item.
 *     @type string      $slug                    Unique URL slug for the nav item.
 *     @type bool|string $item_css_id             Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
 *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
 *                                                member profile other than your own. Default: true.
 *     @type bool        $site_admin_only         Optional. Whether the nav item should be visible only to site admins
 *                                                (those with the 'bp_moderate' cap). Default: false.
 *     @type int         $position                Optional. Numerical index specifying where the item should appear in
 *                                                the nav array. Default: 99.
 *     @type callable    $screen_function         The callback function that will run when the nav item is clicked.
 *     @type bool|string $default_subnav_slug     Optional. The slug of the default subnav item to select when the nav
 *                                                item is clicked.
 * }
 * @return bool|null Returns false on failure.
 */
function bp_core_new_nav_item($args = '')
{
    $defaults = array('name' => false, 'slug' => false, 'item_css_id' => false, 'show_for_displayed_user' => true, 'site_admin_only' => false, 'position' => 99, 'screen_function' => false, 'default_subnav_slug' => false);
    $r = wp_parse_args($args, $defaults);
    // First, add the nav item link to the bp_nav array.
    $created = bp_core_create_nav_link($r);
    // To mimic the existing behavior, if bp_core_create_nav_link()
    // returns false, we make an early exit and don't attempt to register
    // the screen function.
    if (false === $created) {
        return false;
    }
    // Then, hook the screen function for the added nav item.
    bp_core_register_nav_screen_function($r);
    /**
     * Fires after adding an item to the main BuddyPress navigation array.
     * Note that, when possible, the more specific action hooks
     * `bp_core_create_nav_link` or `bp_core_register_nav_screen_function`
     * should be used.
     *
     * @since BuddyPress (1.5.0)
     *
     * @param array $r        Parsed arguments for the nav item.
     * @param array $args     Originally passed in arguments for the nav item.
     * @param array $defaults Default arguments for a nav item.
     */
    do_action('bp_core_new_nav_item', $r, $args, $defaults);
}
/**
 * Add an item to the primary navigation of the specified component.
 *
 * @since 1.1.0
 * @since 2.6.0 Introduced the `$component` parameter.
 *
 * @param array|string $args {
 *     Array describing the new nav item.
 *     @type string      $name                    Display name for the nav item.
 *     @type string      $slug                    Unique URL slug for the nav item.
 *     @type bool|string $item_css_id             Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
 *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
 *                                                member profile other than your own. Default: true.
 *     @type bool        $site_admin_only         Optional. Whether the nav item should be visible only to site admins
 *                                                (those with the 'bp_moderate' cap). Default: false.
 *     @type int         $position                Optional. Numerical index specifying where the item should appear in
 *                                                the nav array. Default: 99.
 *     @type callable    $screen_function         The callback function that will run when the nav item is clicked.
 *     @type bool|string $default_subnav_slug     Optional. The slug of the default subnav item to select when the nav
 *                                                item is clicked.
 * }
 * @param string       $component The component the navigation is attached to. Defaults to 'members'.
 * @return bool|null Returns false on failure.
 */
function bp_core_new_nav_item($args, $component = 'members')
{
    if (!bp_is_active($component)) {
        return;
    }
    $defaults = array('name' => false, 'slug' => false, 'item_css_id' => false, 'show_for_displayed_user' => true, 'site_admin_only' => false, 'position' => 99, 'screen_function' => false, 'default_subnav_slug' => false);
    $r = wp_parse_args($args, $defaults);
    // Validate nav link data.
    $nav_item = bp_core_create_nav_link($r, $component);
    /*
     * To mimic legacy behavior, if bp_core_create_nav_link() returns false, we make
     * an early exit and don't attempt to register the screen function.
     */
    if (false === $nav_item) {
        return false;
    }
    // Then, hook the screen function for the added nav item.
    $hooked = bp_core_register_nav_screen_function($nav_item);
    if (false === $hooked) {
        return false;
    }
    /**
     * Fires after adding an item to the main BuddyPress navigation array.
     * Note that, when possible, the more specific action hooks
     * `bp_core_create_nav_link` or `bp_core_register_nav_screen_function`
     * should be used.
     *
     * @since 1.5.0
     *
     * @param array $r        Parsed arguments for the nav item.
     * @param array $args     Originally passed in arguments for the nav item.
     * @param array $defaults Default arguments for a nav item.
     */
    do_action('bp_core_new_nav_item', $r, $args, $defaults);
}