How to add 3rd party plugin template files to Sage9?

I have a plugin that uses its own templates, in this case single-ajde_events.php. I have copied this .php file and it loads sucessfully from my themename/resources folder.

However, I fail to manage to integrate this template into the blade template structure. I have consulted the eBook, searched the forums, Google, but I only found a couple of topics trying to accomplish this with WooCommerce and Sage9, so I tried the solution presented there:

I added <?php echo App\Template('single-events'); ?> to the template file and created a single-events.blade.php file in the same folder.

What happens is that this seems breaks something. The page does not load anymore and even the loading time of all other pages is increased tremendeously. When I remove the above code and rerun Yarn, everything returns to normal.

What is the proper way in Sage 9 to accomplish this? Basically, I want these template files to redirect to my own blade-templates so I can keep working within the same structure.

1 Like

Hey @manalyse - if the plugin is using a custom post type and its template is named single-ajde_events.php, then there’s a good chance it’s compatible with the normal WordPress template hierarchy. If that’s the case, you can try just using single-ajde_events.blade.php instead of single-ajde_events.php. At that point you can put whatever Blade code in the template you want.

LMK if that works!

1 Like

Hey @mmirus, thanks for your help. Unfortunately, that did not work. It does not find the blade-file. I checked if the plugin uses custom post types, and it does use the custom post type ajde_events.

Is this the plugin? https://codecanyon.net/item/eventon-wordpress-event-calendar-plugin/1211017

Try making an ‘eventon’ subfolder and putting the Blade file in there:

resources/views/eventon/single-ajde_events.blade.php

Looks like that’s where they want you to place your override template files: http://docs.myeventon.com/documentations/customize-single-events-page/.

Yes. I tried that. I have put the file in every folder in the theme, including eventon folders in every subdirectory (theme root, resources, views, partials) to check if that’s the issue - it’s not.

I took a look at the plugin code and this should work for you. Put your single-ajde_events.blade.php file in views, and add this code to app/filters.php:

add_filter('template_include', function ($template) {
    if (is_single() && get_post_type() == 'ajde_events') {
        $blade_template = locate_template('single-ajde_events.blade.php');
        
        return ($blade_template) ? $blade_template : $template;
    }

    return $template;
}, 100);
5 Likes

Thanks a lot! That works.

The problem remains, however, that none of the functions that I find in the original template file, such as do_action('eventon_single_content') seems to work in the blade file.

I tried to do this in controller/app.php:

public function eventonSingle() {
    $eventonSingle = do_action('eventon_single_content');

    return $eventonSingle;
    }

But that does not seem to help either. Am I misunderstanding/overlooking something obvious that’s sage-related, or is it a plugin-related issue?

Can you share a part of your template code where you are calling the do_action (directly in the template, not from the controller)?

Also, are you getting any messages when you try to use those actions (in your browser or your logs)?

1 Like

I have tried two different versions now to test it.

When I try the following in the single-ajde_events.blade.php

<section @php(post_class())>
  <h1>This is the single-ajde_events.blade.php file.</h1>
    <?php do_action('eventon_single_content'); ?>
</section>

It just returns the content of the <h1> tag (I know I can use the @php directive but I wanted to rule out any potential errors related to that).

When I try

<section @php(post_class())>
  <h1>This is the single-ajde_events.blade.php file.</h1>
    {!! $eventonSingle !!}
</section>

I get the error message Undefined variable: eventonSingle in /srv/www/manalyse.de/current/web/app/uploads/cache/07699fdd0b9590b28a3cb1439ecd75d7d1bbb740.php on line 3

It’s also blank (except for the <h1>) when I try

@extends('layouts.app')

@section('content')
  @while(have_posts()) @php(the_post())
      <h1>This is the content-single-ajde_eventsblade.php file.</h1>
        <?php do_action('eventon_single_content'); ?>
  @endwhile
@endsection

No message in the logs.

1 Like

Thanks for the info! Using the @php(do_action('action_name')) syntax is the approach you want (see app.blade.php, for example).

Can’t say for sure since I only have an older version of the plugin for testing, but I don’t think anything is actually supposed to be displayed from those actions out of the box. Instead, you (or addons) could use those actions to do your own thing at those points in the template.

Take a look at the plugin’s template file (single-ajde_events.php) and see what’s in there. In my version, they output the actual content by including content-single-event.php. That’s where it constructs the HTML that’s printed. You would need to include that in your Blade template or write your own template code to output the events details using that partial from the plugin as a starting point (this is probably the point where you would use a controller to prepare the event data for your Blade template).

In case it’s helpful, I’ve included an example of how you might modify app.blade.php and a sample single template to add most of the actions they talk about in their docs. (I didn’t include the sidebar action.)

app.blade.php - adds eventon_before_main_content and eventon_after_main_content:

<!doctype html>
<html @php(language_attributes())>
  @include('partials.head')
  <body @php(body_class())>
    @if (is_single() && get_post_type() == 'ajde_events')
      @php(do_action('eventon_before_main_content'))
    @else
      @php(do_action('get_header'))
    @endif
    @include('partials.header')
    <div class="wrap container" role="document">
      <div class="content">
        <main class="main">
          @yield('content')
        </main>
        @if (App\display_sidebar())
        <aside class="sidebar">
          @include('partials.sidebar')
        </aside>
        @endif
      </div>
    </div>
    @if (is_single() && get_post_type() == 'ajde_events')
      @php(do_action('eventon_after_main_content'))
    @else
      @php(do_action('get_footer'))
    @endif
    @include('partials.footer')
    @php(wp_footer())
  </body>
</html>

And single-ajde_events.blade.php - adds eventon_single_content_wrapper and eventon_single_after_loop:

@extends('layouts.app')

@section('content')
  @php(do_action('eventon_single_content_wrapper'))

  @while(have_posts()) @php(the_post())
    @include('partials.page-header')
    @php(do_action('eventon_single_content'))
    Your template code to output the event details here...
  @endwhile

  @php(do_action('eventon_single_after_loop'))
@endsection
1 Like

Thanks, I have added these codes, but they don’t seem to allow me to call the necessary functions to actually get any of the data. I mean, I am using the same technique to get ACF data to my templates, but it does not seem to work with eventON.

I don’t understand at all why these functions don’t work. I am looking at the content-single-event.php just like you suggest, and this is the code:

<div class='eventon_main_section' >
	<?php do_action('eventon_oneevent_wrapper');?>
		<?php do_action('eventon_oneevent_evodata');?>
		<?php do_action('eventon_oneevent_head');?>		
		<div id='evcal_list' class='eventon_events_list evo_sin_event_list'>
		<?php
			do_action('eventon_oneevent_repeat_header');
			do_action('eventon_oneevent_event_data');
		?>
		</div>
	</div>
</div>

If I copy any of this into my template, nothing gets displayed. Also tried adding these functions via controller app (like I do with ACF as is described in the eBook). Nothing.

1 Like

Oh, yeah, they’ve really cleaned that up since the version I have and looks like they probably moved a lot to the actions that was handled directly in the template in the older version.

Since my version is different I can’t directly help too much beyond this point. :frowning:

But one thing you might try is open up the entire plugin folder in your editor and search for those actions (e.g., search the entire plugin codebase for eventon_oneevent_event_data).

What you want to find is where they have something like add_action('eventon_oneevent_event_data', ...). That will help you track down what happens in the plugin when that action fires. If you find that, you can see what code they are using and might be able to find out why it isn’t working. You may not be able to directly fix it, but you might be able to see what you could add in your controller / template to recreate what they’re doing in a way that works.

You may also be able to contact the plugin author for support to see if he has any advice - not sure how far he’d be willing to go to support overriding the templates in a theme like Sage, but he might be able to give you some pointers, at least.

1 Like

Well, thanks a lot for your help, although it did not solve the problem in the end, it was much appreciated and I have learned a lot!

I think I will have to look for a different solution for this altogether. It is too much work going into the code this deep just to be able to have more stylistic control. Although using the default template creates many other problems.

Actually, this solved the problem! Thanks a thousand times @mmirus, you’re a saint.

By the way: I had not activated the licence yet on the development website, that’s why the functions did not work. Oops.

2 Likes

Haha, I’m glad we got it working in the end!