BuddyPress and Breadcrumb Trail Plugin Compatibility – Updated
If you are using a newer version of BuddyPress(2.6.0 or newer), My old code for adding breadcrumb for BuddyPress components won't work. It is due to the change in the way BuddyPress Nav items are handled now.
A few years ago, I created a better solution for our Community Builder theme and the Breadcrumb Trail plugin. Here is a slightly modified version of that code. It will work with BuddyPress 2.6.0+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | /** * Helper class for adding the trail items to the breadcrumb trail * * @copyright 2018, Brajesh Singh. */ class BuddyDev_BP_Breadcrumb { /** * Add BuddyPress User/group specific trail items * * @param array $trail breadcrumb trail. * * @return array */ public function add_trail_items( $trail ) { if ( bp_is_user() ) { return $this->add_user_trail_items( $trail ); } elseif ( bp_is_group() ) { return $this->add_group_trails( $trail ); } return $trail; } /** * Add BuddyPress User specific trail item depending on the current page * * @param array $trail breadcrumb trail. * * @return array */ public function add_user_trail_items( $trail ) { unset( $trail['trail_end'] ); $bp = buddypress(); // current action. $action = bp_current_action(); $component = bp_current_component(); $action_variables = bp_action_variables(); $displayed_user_id = bp_displayed_user_id(); // add Members Page as Link. $trail[] = '<a href="' . get_permalink( $bp->pages->members->id ) . '">' . get_the_title( $bp->pages->members->id ) . '</a>'; // if we are here, we are most probably on a component screen of user. $trail[] = bp_core_get_userlink( $displayed_user_id ); // get details of current main nav(component). $nav_details = $this->get_user_component_details( $component ); // let us keep the name of current nav menu as end point. $trail_end = $nav_details['name']; // are we doing some action for the component. if ( ! empty( $action ) ) { // yes, then let us link to the parent component on user. $trail[] = '<a href="' . $nav_details['link'] . '">' . $nav_details['name'] . '</a>'; $subnav_details = $this->get_user_action_details( $component, $action ); // let us keep that sub nav action name as the end point. $trail_end = $subnav_details['name']; } if ( ! empty( $action_variables ) ) { // is some action_variable set. // if yes, let us append the parent action link to the breadcrumb. $trail[] = '<a href="' . $subnav_details['link'] . '">' . $subnav_details['name'] . '</a>'; $trail_end = array_pop( $action_variables ); foreach ( $action_variables as $action_name ) { $trail[] = ucwords( str_replace( '-', ' ', $action_name ) ); } $trail_end = ucwords( str_replace( '-', ' ', $trail_end ) ); } if ( ! empty( $trail_end ) ) { $trail['trail_end'] = $trail_end; } return $trail; } /** * Add group specific trail items * * @param array $trail trail items. * * @return array */ public function add_group_trails( $trail ) { $bp = buddypress(); // let us append the group directory page as link. $trail[] = '<a href="' . get_permalink( $bp->pages->groups->id ) . '">' . get_the_title( $bp->pages->groups->id ) . '</a>'; // get the current group details. $group = groups_get_current_group(); $action = bp_current_action(); $action_variables = bp_action_variables(); $trail_end = ''; // if no action is set, we are on group home page. if ( empty( $action ) ) { $trail['trail_end'] = bp_get_group_name( $group ); } else { // we are on any of group internal page. $trail[] = '<a href="' . bp_get_group_permalink( $group ) . '">' . bp_get_group_name( $group ) . '</a>'; $subnav_details = $this->get_group_action_details( $group->slug, $action ); $trail_end = $subnav_details['name']; if ( ! empty( $action_variables ) ) { $trail[] = '<a href="' . $subnav_details['link'] . '">' . $subnav_details['name'] . '</a>'; $trail_end = array_pop( $action_variables ); foreach ( $action_variables as $action_name ) { $trail[] = ucwords( str_replace( '-', ' ', $action_name ) ); } $trail_end = ucwords( str_replace( '-', ' ', $trail_end ) ); } } if ( ! empty( $trail_end ) ) { $trail['trail_end'] = $trail_end; } return $trail; } /** * Get an array containing the details of current action(sub nav ietm details * * @param string $component component. * @param string $action action. * * @return array ( * 'name' => '', * 'link' => '', * 'slug' => '' * ) */ public function get_user_action_details( $component, $action ) { $items = $this->get_user_subnav_items( $component ); foreach ( $items as $item ) { if ( $item['slug'] == $action ) { return $item; } } return array(); } /** * Get the array containing component nav details * * @param string $component component. * * @return array */ private function get_user_component_details( $component ) { $nav_items = $this->get_user_nav_items(); foreach ( $nav_items as $nav_item ) { if ( $nav_item['slug'] == $component ) { return $nav_item; } } return array(); } /** * Get user nav items. * * @return array */ private function get_user_nav_items() { if ( class_exists( 'BP_Core_Nav' ) ) { $nav_items = buddypress()->members->nav->get_primary(); } else { $nav_items = buddypress()->bp_options_nav; } return $nav_items; } /** * Get secondary nav. * * @param string $component component. * * @return array */ private function get_user_subnav_items( $component ) { if ( class_exists( 'BP_Core_Nav' ) ) { $nav_items = buddypress()->members->nav->get_secondary( array( 'parent_slug' => $component ) ); } else { $nav_items = buddypress()->bp_options_nav; } return $nav_items; } /** * Get an array containing current group, current action details * * @param string $group_slug group slug. * @param string $action action. * * @return array */ public function get_group_action_details( $group_slug, $action ) { $nav_items = $this->get_group_nav_items( $group_slug ); foreach ( $nav_items as $nav_item ) { if ( $nav_item['slug'] == $action ) { return $nav_item; } } return array(); } /** * Get nav items array for the current group * * @param string $group_slug group slug. * * @return array */ private function get_group_nav_items( $group_slug = '' ) { if ( class_exists( 'BP_Core_Nav' ) ) { $nav_items = buddypress()->groups->nav->get_secondary( array( 'parent_slug' => $group_slug ) ); } else { $nav_items = buddypress()->bp_options_nav; } return $nav_items; } } /** * Add BuddyPress specific trails to Breadcrumb Trail plugin * * @param array $trail trail items. * @param array $args args. * * @return array */ function buddydev_add_bp_trail_items( $trail, $args ) { if ( ! function_exists( 'buddypress' ) ) { return $trail; } $trail_bp = new BuddyDev_BP_Breadcrumb(); return $trail_bp->add_trail_items( $trail ); } add_filter( 'breadcrumb_trail_items', 'buddydev_add_bp_trail_items', 10, 2 ); |
All we are doing is extracting the details for BuddyPress and adding to the trail items.
It is also available as a gist here
https://gist.github.com/sbrajesh/3e1fe81377e9209d694a188e18c1043f
Have fun!