Roots Discourse

Custom plugin template rendering after </html>. Overriding template duplicates <html> block in page

I have a wordpress site using Sage9. I installed Modern Events Calendar and created an Event page using the plugin.

When rendering the page it shows:

<html>
    [...]
    <header>[...]</header>
    <footer>[...]</footer>
    [...]
</html>
<section id="main-content" class="mec-container">
[...]

Furthermore, overriding the template (creating a single-mec-events.php) within the plugin doesn’t work, it renders two <html> blocks.

Having a single-mec-events.php:

<?php
use function App\template;
echo template('page');

Renders:

<html>
    [...]
    <header>[...]</header>
    <footer>[...]</footer>
    [...]
</html>
<html>
    [...]
    <header>[...]</header>
    <footer>[...]</footer>
    [...]
</html>

This is the plugin template single-mec-events.php:

<?php
/** no direct access **/
defined('MECEXEC') or die();

/**
 * The Template for displaying all single events
 * 
 * @author Webnus <info@webnus.biz>
 * @package MEC/Templates
 * @version 1.0.0
 */
get_header('mec'); ?>

    <section id="<?php echo apply_filters('mec_single_page_html_id', 'main-content'); ?>" class="<?php echo apply_filters('mec_single_page_html_class', 'mec-container'); ?>">

        <?php do_action('mec_before_main_content'); ?>

        <?php while(have_posts()): the_post(); ?>

            <?php $MEC = MEC::instance(); echo $MEC->single(); ?>

        <?php endwhile; // end of the loop. ?>
        <?php comments_template(); ?>
    </section>

    <?php do_action('mec_after_main_content'); ?>

<?php get_footer('mec');

Turns out that the problem was this snippet I was using to make my theme compatible with BuddyPress.

Does someone know why it was creating the conflict? (would be useful to undersand it and to have it documented somewhere)


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

    if (strpos($template, "mec-events") !== false) {
        return $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',
        ]
    );
});

FYI, to fix the conflict between this snippet and MEC templates I just inserted this condition at the beginning of bp_template_include_theme_compat's filter:


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

    if (strpos($template, "mec-events") !== false) {
        return $template;
    }
    [...]

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