Sage 9, Blade and Buddypress

Hi @smutek @MWDelaney
I’m trying to implement Buddypress with the latest dev-master of Sage 9.0.0-beta.3 (April 21st, 2017).
I’ve followed the various solutions here (Woocommerce & Blade (Sage 9) - #27 by Twansparant) to transpose it for Buddypress, without luck. Is there something new needed now that views are in resources/ ?
Here is explained how Buddypress template pack works:
Buddypress theme compat
I’ve tried to create a ‘buddypress’ folder in > resources/views/ buddypress/index.php with something like:
<?php echo App\Template('buddypress');
and, in ‘views’ folder i have created a file ‘buddypress.blade.php’ with this code:

@extends('layouts.app')

    @section('content')
      @while(have_posts()) @php(the_post())
        @include('partials.page-header')
        @include('partials.content-page')
      @endwhile
    @endsection

but it doesn’t work, any help will be much appreciate, because #buddypress is quite a major plugin in Wordpress world :eyeglasses:

When you say it “doesn’t work” what do you mean? What does it do? What doesn’t it do?

I also see that it seems you’re simply doing this

  @include('partials.page-header')
  @include('partials.content-page')

Which will include the Sage page header and content, not the BuddyPress content. What does the BuddyPress theming guide say should go here? For WooCommerce this is @php(woocommerce_content()).

1 Like

I dont think there is an equivalent for ‘woocommerce_content’ in Buddypress Template legacy. I thing BP use ‘the_content’ …
When i activate sage Theme with Buddypress i get a white screen of death and when WP_DEBUG in on true i dont have any php error or warning. Buddypress works with other wp themes on my wp install.

i have replace the code in buddypress.blade.php but it still doesn’t work:

@extends('layouts.base') 

@section('content') 
	@php(the_content())
@endsection

Here is how bp-legacy bp templates are organized:
buddypress GitHub

i think there is something to do with his templates php classes:
https://github.com/buddypress/BuddyPress/blob/master/src/bp-templates/bp-legacy/buddypress-functions.php

Any idea? Thanks for your help

if i try to navigate to a “dynamic view” of buddypress (wth no corresponding wp page) i get a blank screen of death… If i create a ‘buddypress’ folder path the root level of Sage theme folder ‘resources’ with in a file called ‘buddypress.php’ with the following content
<?php echo App\Template('buddypress');

Than i dont have a blank screen and i’m able to display a page but without the buddypress content injected in it. That means buddypress template loader can’t find where to inject his content…

Now, I have managed to display the buddypress “members” page because the plugins settings allow to define a wp page for this “members” screen. If i select the default sage “Custom template” for this “members” page, i’m able to display the corresponding buddypress screen with no php error.

Now i’m stuck, any idea?

Nevermind i have found the solution !

Here is how to make buddypress working withe Sage 9 blade template

  1. in your sage theme folder/ressources/
    Create a ‘buddypress’ folder

  2. in your new ressources/buddypress/ folder, create a file ‘buddypress.php’ with this in it :
    <?php echo App\Template('buddypress');

  3. in in your sage theme folder/ressources/views/
    Create a file called ‘buddypress.blade.php’ with this code in it :

    @extends(‘layouts.app’)

    @section(‘content’)
    @while(have_posts()) @php(the_post())
    @include(‘partials.page-header’)
    @include(‘partials.content-page’)
    @endwhile
    @endsection

Thats it, now Sage 9 is compatible with Buddypress
With this, you don’t have to change the default template for buddypress pages like ‘members’ or groups’ defined in wp admin Buddypress settings.

Hope that helps someone else!

6 Likes

UPDATE

@MWDelaney
To enable buddypress template compatibility to Sage 9,
another (better) solution i use now is to add this filter to app/filters.php :

/**
 * change the default page template selection
 * needed for buddypress template compatibility
 */

add_filter('template_include', function ($template) {
    
    global $post;
    
    if (is_singular( 'page' )) {
        $default_template = locate_template( array( 'template-custom.blade.php' ) );
        if ( '' != $default_template 
            && '' == $post->page_template // Only when page_template is not set
        ){
            //return $default_template;
            $template = $default_template;
        }
    }

    return $template;
}, 99);

Hope it helps!

2 Likes

A little less frightening of a solution:

add_filter('bp_template_include_theme_compat', function() {
    echo template('page');
    return '';
});

I personally use singular, but page should work out of the box for Sage. Unless you are using add_theme_support('buddypress')– BuddyPress just tries to override the_content() in anyway it can, so a generic page view should work as intended.

2 Likes

Did you find a way to make this work for Memberpress?

This seems to be the offending Memberpress function

  // Template selection
  public static function template_include($template) {
    global $post, $wp_query;

    if(!is_singular()) { return $template; }

    if(isset($post) && is_a($post, 'WP_Post') && $post->post_type == MeprProduct::$cpt) {
      $product = new MeprProduct($post->ID);
      $new_template = $product->get_page_template();
    }

    if(isset($new_template) && !empty($new_template)) { return $new_template; }

    return $template;
  }

Edit:
Actually I was able to get a Memberpress Registration page loading by slightly modifying the code @mecanographik wrote:

add_filter('template_include', function ($template) {

    global $post;

    if(isset($post) && is_a($post, 'WP_Post')) {

        $default_template = locate_template( array( 'template-custom.blade.php' ) );
        if ( '' != $default_template
            && '' == $post->page_template // Only when page_template is not set
        ){
            //return $default_template;
            $template = $default_template;
        }
    }

    return $template;
}, 99);

Actually I left out some code in the filter that was preventing Sage from correctly loading templates. Here’s the corrected version:

add_filter('template_include', function ($template) {

    global $post;

    if(isset($post) && is_a($post, 'WP_Post') && $post->post_type == \MeprProduct::$cpt) {

        $default_template = locate_template( array( 'template-custom.blade.php' ) );
        if ( '' != $default_template
            && '' == $post->page_template // Only when page_template is not set
        ){
            //return $default_template;
            $template = $default_template;
        }
    }

    return $template;
}, 99);
1 Like

Hi @mecanographik,

I tried implement with your step, and I added add_theme_support(‘buddypress’) in setup.php file. But Buddypress is not work in my theme. Buddypress can not overwrite the_content when I go to page template such as domain.com/groups, domain.com/activity

Have you any missing some think in your step?

Hi,

I removed add_theme_support(‘buddypress’) in my config and it worked well

I’ve read all of the above and various solutions partly work but is there an official stance on how to integrate BuddyPress the correct way?

@Log1x - this solution works but any page that uses a custom template shows up as a white screen …

add_filter('bp_template_include_theme_compat', function() {
    echo template('page');
    return '';
});

@mecanographik - this solution works but i have to list all my custom page templates (which is not the end of the world of course!)

add_filter('template_include', function ($template) {
    global $post;
    if (is_singular( 'page' )) {
        $default_template = locate_template( array( 'XXX.blade.php', 'YYY.blade.php' ) );
        if ( '' != $default_template 
            && '' == $post->page_template // Only when page_template is not set
        ){
            //return $default_template;
            $template = $default_template;
        }
    }
    return $template;
}, 99);

The solution that works best is just adding a buddypress folder to /resources/, creating buddypress.php and in it adding echo App\Template('buddypress'); and creating buddypress.blade.php in /views/ - this works naively for all my scenarios.

The big issue i have now is that i can’t seem to target this template with any controllers. I create Controllers/BuddyPress and i get an error about re-declaring a class. Even if i use echo App\Template('page'); in resources/buddypress/buddypress.php - i can’t output from my Controllers/Page either - it’s like the way I’ve implemented this doesn’t have any controller support (http://prntscr.com/o1hyyt)?

As an update - i’ve just added buddypress.blade.php to that $default_template array = …

add_filter('template_include', function ($template) {
    global $post;
    if (is_singular( 'page' )) {
        $default_template = locate_template( array( 'buddypress.blade.php' ) );
        if ( '' != $default_template 
            && '' == $post->page_template // Only when page_template is not set
        ){
            //return $default_template;
            $template = $default_template;
        }
    }
    return $template;
}, 99);

… and i seem to have control now with my Page controller!

So i seem to have stumbled across a solution - but i haven’t a clue what i’m doing to be honest! I’d really like to implement the ‘right’ way if anyone has figured it out?

Any help would be hugely appreciated.

1 Like

Just jumping into Sage dev so I’m not sure that this is correct but thought I’d share what I’m doing. Basically, I’m just limiting the template to BuddyPress pages. So far all other templates seem to load ok for me. Haven’t gotten into playing with page controllers yet so I’m not sure how this’ll work to that end.

add_filter('bp_template_include_theme_compat', function($template) {
    if (is_buddypress()) {
        echo template('page');
        $template = '';
    }
    return $template;
});

thanks @evan for your response - this solution works well until i try to access the Controller/Page functions / data which are all blanked?

$data = apply_filters('sage/template/app-data/data', []);
echo template('template', $data);

Notice app-data. This is the body class relating to your controller.

I decided to move this out the discussion to make it more easily finable: