bp_core_create_nav_link

Add a link to the main BuddyPress navigation.

Description

(bool|BP_Core_Nav_Item) bp_core_create_nav_link( (string) $args = '', (string) $component = 'members' ); 

Returns (bool|BP_Core_Nav_Item)

Returns false on failure, new nav item on success.

Parameters (2)

0. $args — Optional. (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'
Component that the nav belongs to.

Usage

  1. if ( !function_exists( 'bp_core_create_nav_link' ) ) { 
  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. // Optional. Component that the nav belongs to. 
  16. $component = 'members'; 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = bp_core_create_nav_link($args, $component); 
  20.  

Defined (1)

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

/bp-core/bp-core-buddybar.php  
  1. function bp_core_create_nav_link( $args = '', $component = 'members' ) { 
  2. $bp =buddypress); 
  3.  
  4. $defaults = array( 
  5. 'name' => false, // Display name for the nav item. 
  6. 'slug' => false, // URL slug for the nav item. 
  7. 'item_css_id' => false, // The CSS ID to apply to the HTML of the nav item. 
  8. 'show_for_displayed_user' => true, // When viewing another user does this nav item show up? 
  9. 'site_admin_only' => false, // Can only site admins see this nav item? 
  10. 'position' => 99, // Index of where this nav item should be positioned. 
  11. 'screen_function' => false, // The name of the function to run when clicked. 
  12. 'default_subnav_slug' => false // The slug of the default subnav item to select when clicked. 
  13. ); 
  14.  
  15. $r = wp_parse_args( $args, $defaults ); 
  16.  
  17. // If we don't have the required info we need, don't create this nav item. 
  18. if ( empty( $r['name'] ) || empty( $r['slug'] ) ) { 
  19. return false; 
  20.  
  21. // If this is for site admins only and the user is not one, don't create the nav item. 
  22. if ( ! empty( $r['site_admin_only'] ) && ! bp_current_user_can( 'bp_moderate' ) ) { 
  23. return false; 
  24.  
  25. if ( empty( $r['item_css_id'] ) ) { 
  26. $r['item_css_id'] = $r['slug']; 
  27.  
  28. $nav_item = array( 
  29. 'name' => $r['name'],  
  30. 'slug' => $r['slug'],  
  31. 'link' => trailingslashit( bp_loggedin_user_domain() . $r['slug'] ),  
  32. 'css_id' => $r['item_css_id'],  
  33. 'show_for_displayed_user' => $r['show_for_displayed_user'],  
  34. 'position' => $r['position'],  
  35. 'screen_function' => &$r['screen_function'],  
  36. 'default_subnav_slug' => $r['default_subnav_slug'] 
  37. ); 
  38.  
  39. // Add the item to the nav. 
  40. buddypress)->{$component}->nav->add_nav( $nav_item ); 
  41.  
  42. /** 
  43. * Fires after a link is added to the main BuddyPress nav. 
  44. * @since 2.4.0 
  45. * @since 2.6.0 Added `$component` parameter. 
  46. * @param array $r Parsed arguments for the nav item. 
  47. * @param array $args Originally passed in arguments for the nav item. 
  48. * @param array $defaults Default arguments for a nav item. 
  49. * @param string $component Component that the nav belongs to. 
  50. */ 
  51. do_action( 'bp_core_create_nav_link', $r, $args, $defaults, $component ); 
  52.  
  53. return $nav_item;