bp_core_new_nav_item

Add an item to the primary navigation of the specified component.

Description

(bool|null) bp_core_new_nav_item( (array|string) $args, (string) $component = 'members' ); 

Returns (bool|null)

Returns false on failure.

Parameters (2)

0. $args (array|string)
Array describing the new nav item.

Options

  • name (string) => ''

    Display name for the nav item.

  • slug (string) => ''

    Unique URL slug for the nav item.

  • item_css_id (bool|string) => ''

    id attribute for the nav item. Default: the value of $slug..

  • show_for_displayed_user (bool) => null

    Whether the nav item should be visible when viewing a member profile other than your own. Default: true.

  • site_admin_only (bool) => null

    Whether the nav item should be visible only to site admins (those with the bp_moderate cap). Default: false.

  • position (int) => 0

    Numerical index specifying where the item should appear in the nav array. Default: 99.

  • screen_function (callable) => null

    The callback function that will run when the nav item is clicked.

array(

    /**
     * Display name for the nav item.
     *
     * @type string
     * @default ''
     */
    'name' => '',

    /**
     * Unique URL slug for the nav item.
     *
     * @type string
     * @default ''
     */
    'slug' => '',

    /**
     * Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
     *
     * @type bool|string
     * @default ''
     * @optional
     */
    'item_css_id' => '',

    /**
     * Optional. Whether the nav item should be visible when viewing a member profile other than your
     * own. Default: true.
     *
     * @type bool
     * @default null
     * @optional
     */
    'show_for_displayed_user' => null,

    /**
     * Optional. Whether the nav item should be visible only to site admins (those with the
     * 'bp_moderate' cap). Default: false.
     *
     * @type bool
     * @default null
     * @optional
     */
    'site_admin_only' => null,

    /**
     * Optional. Numerical index specifying where the item should appear in the nav array. Default:
     * 99.
     *
     * @type int
     * @optional
     */
    'position' => 0,

    /**
     * The callback function that will run when the nav item is clicked.
     *
     * @type callable
     * @default null
     */
    'screen_function' => null
);        

1. $component — Optional. (string) => 'members'
The component the navigation is attached to. Defaults to members..

Usage

  1. if ( !function_exists( 'bp_core_new_nav_item' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-buddybar.php'; 
  3.  
  4. // Array describing the new nav item. 
  5. $args = array( 
  6. 'name' => '', 
  7. 'slug' => '', 
  8. 'item_css_id' => '', 
  9. 'show_for_displayed_user' => null, 
  10. 'site_admin_only' => null, 
  11. 'position' => 0, 
  12. 'screen_function' => null 
  13. ); 
  14.  
  15. // The component the navigation is attached to. Defaults to 'members'. 
  16. $component = 'members'; 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = bp_core_new_nav_item($args, $component); 
  20.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-buddybar.php  
  1. function bp_core_new_nav_item( $args, $component = 'members' ) { 
  2. if ( ! bp_is_active( $component ) ) { 
  3. return; 
  4.  
  5. $defaults = array( 
  6. 'name' => false, // Display name for the nav item. 
  7. 'slug' => false, // URL slug for the nav item. 
  8. 'item_css_id' => false, // The CSS ID to apply to the HTML of the nav item. 
  9. 'show_for_displayed_user' => true, // When viewing another user does this nav item show up? 
  10. 'site_admin_only' => false, // Can only site admins see this nav item? 
  11. 'position' => 99, // Index of where this nav item should be positioned. 
  12. 'screen_function' => false, // The name of the function to run when clicked. 
  13. 'default_subnav_slug' => false // The slug of the default subnav item to select when clicked. 
  14. ); 
  15.  
  16. $r = wp_parse_args( $args, $defaults ); 
  17.  
  18. // Validate nav link data. 
  19. $nav_item = bp_core_create_nav_link( $r, $component ); 
  20.  
  21. /** 
  22. * To mimic legacy behavior, if bp_core_create_nav_link() returns false, we make 
  23. * an early exit and don't attempt to register the screen function. 
  24. */ 
  25. if ( false === $nav_item ) { 
  26. return false; 
  27.  
  28. // Then, hook the screen function for the added nav item. 
  29. $hooked = bp_core_register_nav_screen_function( $nav_item ); 
  30. if ( false === $hooked ) { 
  31. return false; 
  32.  
  33. /** 
  34. * Fires after adding an item to the main BuddyPress navigation array. 
  35. * Note that, when possible, the more specific action hooks 
  36. * `bp_core_create_nav_link` or `bp_core_register_nav_screen_function` 
  37. * should be used. 
  38. * @since 1.5.0 
  39. * @param array $r Parsed arguments for the nav item. 
  40. * @param array $args Originally passed in arguments for the nav item. 
  41. * @param array $defaults Default arguments for a nav item. 
  42. */ 
  43. do_action( 'bp_core_new_nav_item', $r, $args, $defaults );