Plugin view override with .blade.php

We are working on a website and for this site we are using a plugin for brokers to show the houses, it’s a commercial plugin which is not available in the WordPress plugin store.

The plugin is working with views which I can override.

Currently the folder for the plugin is in resources/view/[plugin name]/single.php etc.

The extension is working, but not with the blade.php ecosystem. Is there a way to get this working, so it will listen to the .blade.php extension as well?

What steps do I need to do to get this working. The plugin makers do not support Roots Sage, so from them I will not get support. I’m kinda stuck right now…

I’m not sure you can make the plugin listen to Blade templates since these two are completely separate.

One solution I can think of to solve this is to render the template using \Roots\view('path', [])->render() inside the plugin’s normal template.

3 Likes

It depends on how the plugin you are talking about is built. But if they have filters for the template lookups then you can extend them using a plugin.

I’d recommend checking out the woocommerce and events calendar plugins for sage compatibility.

https://github.com/supermundano/sage-the-events-calendar

1 Like

Thanks! I’m kinda stuck on this… Can anybody help me on this?

In what way are you stuck? We will need more details.

However, what you’re trying to do may be more trouble than it’s worth. What is the downside to just using the plugin’s typical template overrides?

My plugin is listening to the following directory files

For single

 /resources/views/wonen/single.php

For the archive

 /resources/views/wonen/archive.php

The default single.blade.php is ignored, instead it gives default WP head and footer, so it ignores everything i’ve setup in my theme.

Below a example of the archive.php inside the folder of the plugin.

<?php get_header(); ?>

<?php wp_enqueue_script('wonen-archive') ?>
<?php wp_localize_script('wonen-archive', 'Wonen', array(
	'themingurl' => get_stylesheet_directory_uri(),
	//'maps' => Wonen::maps('all'), // Can be used to fetch all coordinates.
)); ?>

<?php global $wp_query; ?>
<h1><?= $wp_query->found_posts ?> <?= ($wp_query->found_posts > 1) ?'woningen':'woning'; ?> gevonden</h1>

<?php if (Wonen::isArchive(array('type' => 'koop'))): ?>
	<p>Alleen koopwoningen worden getoond.</p>
<?php elseif (Wonen::isArchive(array('type' => 'huur'))): ?>
	<p>Alleen huurwoningen worden getoond.</p>
<?php endif; ?>

<?php $woning_count_year = Wonen::whereSql('datumInvoer', '> DATE_SUB(NOW(), INTERVAL 1 YEAR)')->total() ?>
<?php $woning_count_month = Wonen::whereSql('datumInvoer', '> DATE_SUB(NOW(), INTERVAL 1 MONTH)')->total() ?>
<?php $woning_count_week = Wonen::whereSql('datumInvoer', '> DATE_SUB(NOW(), INTERVAL 1 WEEK)')->total() ?>

<?php if ($woning_count_year > 0): ?>
    <?php if ($woning_count_year === 1): ?>
	<p>Er is <?= $woning_count_week ?> nieuwe woning toegevoegd in het laatste jaar.</p>
	<?php else: ?>
	<p>Er zijn <?= $woning_count_year ?> nieuwe woningen toegevoegd in het laatste jaar.</p>
	<?php endif; ?>
<?php endif; ?>
<?php if ($woning_count_month > 0): ?>
    <?php if ($woning_count_month === 1): ?>
	<p>Er is <?= $woning_count_week ?> nieuwe woning toegevoegd deze maand.</p>
	<?php else: ?>
	<p>Er zijn <?= $woning_count_month ?> nieuwe woningen toegevoegd deze maand.</p>
	<?php endif; ?>
<?php endif; ?>
<?php if ($woning_count_week > 0): ?>
    <?php if ($woning_count_week === 1): ?>
	<p>Er is <?= $woning_count_week ?> nieuwe woning toegevoegd deze week.</p>
	<?php else: ?>
	<p>Er zijn <?= $woning_count_week ?> nieuwe woningen toegevoegd deze week.</p>
	<?php endif; ?>
<?php endif; ?>

<form id="entity-search-form" method="GET" action="">
	<div id="entity-search" class="search-sidebar">
		<?= Wonen::template('search'); ?>
	</div>

	<div id="entity-results" class="search-content <?= array_get($_COOKIE, 'display', 'list'); ?>">
		<?= Wonen::template('loop'); ?>
	</div>
</form>
<script type="text/javascript">
// Manully add a loading overview if a custom search page is to be loaded, to avoid flickering at load
if (location.hash) document.getElementById('entity-search-form').className = 'search-loading';
</script>

<?php get_footer(); ?>

For Roots Sage 9 I used this filter, but for Roots Sage 10 this ain’t working anymore :frowning:

add_filter('template_include', function ($template) {
    if (is_single() && get_post_type() == 'myposttype') {
        $blade_template = locate_template('single-myposttype.blade.php');

        return ($blade_template) ? $blade_template : $template;
    }

    if (is_archive() && get_post_type() == 'myposttype') {
        $blade_template = locate_template('archive-myposttype.blade.php');

        return ($blade_template) ? $blade_template : $template;
    }

    return $template;
}, 100);

niznet has the answer here. Rather than trying to get your plugin to load a blade template, you can render a blade template in the plugin’s traditional php template.

You’d have two files: one in the plugin’s default location, the contents of which call the second file, a full blade template which does whatever you need it to do.

I found it out myself. :slight_smile:

On the PHP template files from the plugin just add it like the example below.

<?php echo \Roots\view('sections.header')->render(); ?>

2 Likes