themosis / plugin

Themosis framework plugin boilerplate.
54 stars 19 forks source link

Prevent standard template loading on view #9

Closed mickaelperrin closed 7 years ago

mickaelperrin commented 7 years ago

Hi,

I started to use themosis to create a new plugin in an existing wordpress website that uses a traditionnal template.

I was wondering if this is possible to prevent the loading of the standard template when we call a view in the controller.

Thanks

jlambe commented 7 years ago

Do you mean a page or post type template assigned ?

The framework routing is always handled before the WordPress templating system. So you could basically define a route regarding the assigned template and overwrite its content/view from the controller.

mickaelperrin commented 7 years ago

I have defined a custom route, calling a method of my controller that returns a view.

However, the home page is displayed after the content of the view.

Currently, I defined a global variable in the controller before rendering the view and check this variable in the template file of my home page, to avoid its display .

I was wondering if there was a better way to handle this.

jlambe commented 7 years ago

Do you have control over the theme? or you would like to not interfere with the theme code?

mickaelperrin commented 7 years ago

In fact what I would love that when the themosis framework return a view, the "standard" theme is not called as I manage headers and footers in twig directly.

jlambe commented 7 years ago

Ok I see the issue... Routing is triggered from the template_redirect at a priority of 20. One way could be to attach another template_redirect action at a priority of 21, check the is_page_template() condition if it returns your template and then die the process using the wp_die() function.

I haven't tested this but in theory it should work. let me know how it goes.

mickaelperrin commented 7 years ago

Thx, that's a really interesting idea.

However, I didn't get the point. get_page_template returns the default page.php. What I would like is to check if a $response was already sent from a view of the themosis plugin.

In the context of the hook, I have only access to global variables. I saw that the themosis container is stored there, could it be possible to grab some interesting info from there ?

jlambe commented 7 years ago

From the template_redirect hook, you'll need to capture the current WP_Post instance using global $post.

Then you can call the get_page_template_slug($post->ID) to check if current viewed page has a template assigned. The method above return the slug/key name of the attached template.