How-To: Add BuddyPress support to Sage 9

As I need BuddyPress for a project and didn’t find a proper solution here, I took some time to follow different approaches. The requirements were the following:

  • Follows the BP templates hierarchie
  • Allows BP templates overwrites
  • Supports Blade BP templates
  • Supports Controller e.g. to set-up “Buddypress”, “Directory” or “Blogs”-Controllers

So I deep dived into BP today and got it working with the requirements above. I took quite a time to maximize the compatibility to other set-ups as much as possible. Happy to share it here and hopefully to get some feedback how this could get further improved. (What about an official “roots/sage-buddypress” repo? :slight_smile:)

1. Copy the following to filters.php in your-theme/app:

/**
 * Adds BuddyPress compatibility
 */
add_filter( 'bp_template_include_theme_compat', function ( $template ) {

    // Find any oif the templates mentioned by BP codex
    $theme_template = locate_template( [
        'plugin-buddypress',
        'buddypress',
        'community',
        'generic',
        'page',
        'single',
        'singular',
        'index'
    ] );

    // Load template from theme if it exists
    if ( $theme_template ) {
        $data = collect(get_body_class())->reduce(function ($data, $class) {
            return apply_filters("sage/template/{$class}/data", $data);
        }, []);

        echo template( $theme_template, $data );
        return get_stylesheet_directory() . '/index.php';
    }

    return $template;

}, PHP_INT_MAX, 1 );

/**
 * Prepares BuddyPress classes for usage with Controller
 */
add_filter( 'body_class', function ( array $classes ) {
    if ( function_exists('bp_get_the_body_class') ) {
        $bp_classes = array_filter( bp_get_the_body_class() );

        if( !empty( $bp_classes) && is_array( $bp_classes) ) {
            foreach ( $bp_classes as $bp_class ) {
                // We need to append 'bp-' to classes like 'directory'.
                // It will help to sort the Controller Classes and avoids duplicated classes:
                // e.g. class BpDirectory for 'bp-directory-data'
                $prepend = preg_match('/bp|buddypress/i', $bp_class) ? '' : 'bp-';
                $classes[] = $prepend . $bp_class . '-data';
            }
        }
    }

    return $classes;
}, 100);

/**
 * Adds the resources/views folder as potential
 * parent of buddypress/community folder
 */
add_filter('bp_get_template_locations', function( $locations ) {
    return array_merge(
        $locations,
        [
            'views/buddypress',
            'views/community',
        ]
    );
});

/**
 * Checks for overwrites in theme folders and
 * renders Blade templates if available
 */
add_filter( 'bp_get_template_part', function ( $templates, $slug, $name ) {

    foreach ( $templates as $key => $template ) {
        $theme_template = locate_template( $template );

        if ( $theme_template ) {
            $data = collect(get_body_class())->reduce(function ($data, $class) {
                return apply_filters("sage/template/{$class}/data", $data);
            }, []);

            echo template($theme_template, $data);

            return get_stylesheet_directory() . '/index.php';
        }
    }

    return $templates;

}, PHP_INT_MAX, 3);

2. Overwrite BP templates
Add either the folder views/buddypres or views/community and place the files to overwrite in there.

3. Main template file
There is no need to add a buddypress(.blade).php or community(.blade).php file into the /views folder. The template parts are loaded in one of the existing standard files such as page.blade.php or index.php. However you can if you want!

4. Controller Support
BP adds useful classes we can use to add specific Controllers to directories, profiles or all buddypress pages. The code above allows to add app/Controllers/Buddypress.php which is loaded on all BP pages. All other body classes are prefixed with ‘bp-’ (bp-directory-data, bp-user-data, …) so that the classes are BpDirectory or BpUser. This avoids conflicting class names and helps sorting.

5 Likes

This topic was automatically closed after 42 days. New replies are no longer allowed.