Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/14/2013 11:28:49 PM (10 years ago)
Author:
boonebgorges
Message:

Introduce a BuddyPress links box to nav-menus.php

The new BuddyPress metabox on Dashboard > Appearance > Menus allows site
administrators to build navigation menus that include dynamic, user-specific
BuddyPress links. Features:

  • Separate sets of links for logged-in and logged-out users.
  • Logged-in user links are automatically generated based on the logged-in user. For example, adding a 'Settings' item from the BuddyPress menu will create a menu item that points to the logged-in user's Settings page, and will not be shown to logged-out visitors.
  • The list of available Logged-In links is automatically populated with all components that are registered in the BuddyPress navigation (ie, those items that are visible as top-level nav links when viewing a user's profile).
  • Log In and Register links are visible only to logged-out users.

Fixes #5122

Props imath, r-a-y

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-filters.php

    r7416 r7427  
    387387add_filter( 'bp_modify_page_title', 'convert_chars'   );
    388388add_filter( 'bp_modify_page_title', 'esc_html'        );
     389
     390/**
     391 * Add BuddyPress-specific items to the wp_nav_menu.
     392 *
     393 * @since BuddyPress (1.9.0)
     394 *
     395 * @param WP_Post $menu_item The menu item.
     396 * @return obj The modified WP_Post object.
     397 */
     398function bp_setup_nav_menu_item( $menu_item ) {
     399    if ( is_admin() ) {
     400        return $menu_item;
     401    }
     402
     403    // We use information stored in the CSS class to determine what kind of
     404    // menu item this is, and how it should be treated
     405    $css_target = preg_match( '/\sbp-(.*)-nav/', implode( ' ', $menu_item->classes), $matches );
     406
     407    // If this isn't a BP menu item, we can stop here
     408    if ( empty( $matches[1] ) ) {
     409        return $menu_item;
     410    }
     411
     412    switch ( $matches[1] ) {
     413        case 'login' :
     414            if ( is_user_logged_in() ) {
     415                $menu_item->_invalid = true;
     416            } else {
     417                $menu_item->url = wp_login_url( wp_guess_url() );
     418            }
     419
     420            break;
     421
     422        case 'logout' :
     423            if ( ! is_user_logged_in() ) {
     424                $menu_item->_invalid = true;
     425            } else {
     426                $menu_item->url = wp_logout_url( wp_guess_url() );
     427            }
     428
     429            break;
     430
     431        // Don't show the Register link to logged-in users
     432        case 'register' :
     433            if ( is_user_logged_in() ) {
     434                $menu_item->_invalid = true;
     435            }
     436
     437            break;
     438
     439        // All other BP nav items are specific to the logged-in user,
     440        // and so are not relevant to logged-out users
     441        default:
     442            if ( is_user_logged_in() ) {
     443                $menu_item->url = bp_nav_menu_get_item_url( $matches[1] );
     444            } else {
     445                $menu_item->_invalid = true;
     446            }
     447
     448            break;
     449    }
     450
     451    // Highlight the current page
     452    $current = bp_get_requested_url();
     453    if ( strpos( $current, $menu_item->url ) !== false ) {
     454        $menu_item->classes[] = 'current_page_item';
     455    }
     456
     457    return $menu_item;
     458}
     459add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
Note: See TracChangeset for help on using the changeset viewer.