bp_core_new_nav_default

Modify the default subnav item that loads when a top level nav item is clicked.

Description

bp_core_new_nav_default( (string) $args = '' ); 

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Options

  • parent_slug (string) => ''

    The slug of the nav item whose default is being changed.

  • screen_function (callable) => null

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

array(

    /**
     * The slug of the nav item whose default is being changed.
     *
     * @type string
     * @default ''
     */
    'parent_slug' => '',

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


Usage

  1. if ( !function_exists( 'bp_core_new_nav_default' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-buddybar.php'; 
  3.  
  4. // The args. 
  5. $args = array( 
  6. 'parent_slug' => '', 
  7. 'screen_function' => null 
  8. ); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_core_new_nav_default($args); 
  12.  

Defined (1)

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

/bp-core/bp-core-buddybar.php  
  1. function bp_core_new_nav_default( $args = '' ) { 
  2. $bp =buddypress); 
  3.  
  4. $defaults = array( 
  5. 'parent_slug' => false, // Slug of the parent. 
  6. 'screen_function' => false, // The name of the function to run when clicked. 
  7. 'subnav_slug' => false // The slug of the subnav item to select when clicked. 
  8. ); 
  9.  
  10. $r = wp_parse_args( $args, $defaults ); 
  11.  
  12. // This is specific to Members - it's not available in Groups. 
  13. $parent_nav = $bp->members->nav->get_primary( array( 'slug' => $r['parent_slug'] ), false ); 
  14.  
  15. if ( ! $parent_nav ) { 
  16. return ; 
  17.  
  18. $parent_nav = reset( $parent_nav ); 
  19.  
  20. if ( ! empty( $parent_nav->screen_function ) ) { 
  21. // Remove our screen hook if screen function is callable. 
  22. if ( is_callable( $parent_nav->screen_function ) ) { 
  23. remove_action( 'bp_screens', $parent_nav->screen_function, 3 ); 
  24.  
  25. // Edit the screen function for the parent nav. 
  26. $bp->members->nav->edit_nav( array( 
  27. 'screen_function' => &$r['screen_function'],  
  28. 'default_subnav_slug' => $r['subnav_slug'],  
  29. ), $parent_nav->slug ); 
  30.  
  31. if ( bp_is_current_component( $parent_nav->slug ) ) { 
  32.  
  33. // The only way to tell whether to set the subnav is to peek at the unfiltered_uri 
  34. // Find the component. 
  35. $component_uri_key = array_search( $parent_nav->slug, $bp->unfiltered_uri ); 
  36.  
  37. if ( false !== $component_uri_key ) { 
  38. if ( ! empty( $bp->unfiltered_uri[$component_uri_key + 1] ) ) { 
  39. $unfiltered_action = $bp->unfiltered_uri[$component_uri_key + 1]; 
  40.  
  41. // No subnav item has been requested in the URL, so set a new nav default. 
  42. if ( empty( $unfiltered_action ) ) { 
  43. if ( ! bp_is_current_action( $r['subnav_slug'] ) ) { 
  44. if ( is_callable( $r['screen_function'] ) ) { 
  45. add_action( 'bp_screens', $r['screen_function'], 3 ); 
  46.  
  47. $bp->current_action = $r['subnav_slug']; 
  48. unset( $bp->canonical_stack['action'] ); 
  49.  
  50. // The URL is explicitly requesting the new subnav item, but should be 
  51. // directed to the canonical URL. 
  52. } elseif ( $unfiltered_action == $r['subnav_slug'] ) { 
  53. unset( $bp->canonical_stack['action'] ); 
  54.  
  55. // In all other cases (including the case where the original subnav item 
  56. // is explicitly called in the URL), the canonical URL will contain the 
  57. // subnav slug. 
  58. } else { 
  59. $bp->canonical_stack['action'] = bp_current_action(); 
  60.  
  61. return;