Modifying the bbPress Breadcrumb

I am currently working on building the Professional Weaver Society‘s website and I needed to change the breadcrumbs added by bbPress to match the breadcrumbs generated by WooCommerce.

The main reason I have for doing this is that WooCommerce does not properly display the page trail when in the forum section of the website.

Below you will see all of the settings and their defaults, you can change any of them as you need, in this case, I have changed them to match the default WooCommerce breadcrumbs. I used a class called ModifybbPress, this is a single class plugin that I use to make all modifications to bbPress, you can put this class in a plugin file, or you can make it a function and put it in your theme’s functions.php file. Lines containing 63 & 70 are commented out because the function dynamically build those fields, and it’s best not to mess with them unless you really want to change them.

/**
 * Class ModifybbPress
 * @package professional_weaver_suite
 */
class ModifybbPress {

  /**
   * ModifybbPress constructor.
   */
  public function __construct() {
    // Modify the bbPress breadcrumbs.
    add_filter('bbp_before_get_breadcrumb_parse_args', array($this, 'pws_breadcrumb_options'));
  }

  /**
   * Settings for how the bbPress breadcrumbs are displayed.
   * All possible settings & defaults are contained in this method.
   * They have been changed to match up to WooCommerce's breadcrumbs.
   * Found in includes/common/template.php/bbp_get_breadcrumb()
   *
   * @return mixed
   */
  function pws_breadcrumb_options() {
    // HTML before the breadcrumb.
    // Default : <div class="bbp-breadcrumb"><p>
    $args['before'] = '<div class="bbp-breadcrumb">';
    // HTML after the breadcrumb.
    // Default : </p></div>
    $args['after'] = '</div>';

    // Breadcrumb Separator.
    // Default : `>` for RTL & `<` for LTR
    $args['sep'] = '/';
    // How many spaces between the crumb & the Separator.
    // Default : 1
    $args['pad_sep'] = 1;
    // HTML content before the separator.
    // Default : <span class="bbp-breadcrumb-sep">
    $args['sep_before'] = '';
    // HTML content after the separator.
    // Default : </span>
    $args['sep_after'] = '';

    // HTML content before each crumb.
    // Default : empty string.
    $args['crumb_before'] = '';
    // HTML content after each crumb.
    // Default : empty string.
    $args['crumb_after'] = '';

    // Show the homepage in the breadcrumb?
    // Default : true
    $args['include_home'] = true;
    // Manually set Text for website root breadcrumb.
    // Default : title of homepage
    $args['home_text'] = 'Home';

    // Show forum root in breadcrumb?
    // Default : true
    $args['include_root'] = false;
    // Manually set forum archive crumb text.
    // Default : title of forum archive page.
    # $args['root_text'] = '';

    // Show the current page in breadcrumb?
    // Default : true
    $args['include_current'] = true;
    // Manually set the current page crumb.
    // Default : title of current page.
    # $args['current_text'] = '';
    // HTML content before the current crumb.
    // Default : <span class="bbp-breadcrumb-current">
    $args['current_before'] = '';
    // HTML content after the current crumb.
    // Default : </span>
    $args[''] = '';

    return $args;
  }
}

Along with this you will need to let WordPress know when to swap out one breadcrumb for another, I have disabled the default breadcrumbs in both WooCommerce & bbPress and moved their respective function calls into my header.php file.

Below is the logic that determines which breadcrumb to show.

// This is one way to do it…
if( is_bbpress() ) bbp_breadcrumb();
if( ! is_bbpress() ) woocommerce_breadcrumb();

// and this is another…
if( is_bbpress() ) {
  bbp_breadcrumb();
} else {
  woocommerce_breadcrumb();;
}

Leave a Reply

Your email address will not be published. Required fields are marked *