Utilize Theme in Custom "Virtual" Page?

Hello, I’m working on a custom plugin that essentially will generate pages on the fly for us and take parts of the URL to generate the right data.

For example: the permalink /mmr/september2019 would trigger this plugin because it starts with “/mmr/” … The plugin would take that “september2019” and use it to generate the correct information to display on the page.

From there, I want the page to pull in the X theme’s default template for our site and place the generated content inside the template.

public function virtualMmrPage(){
	global $wp;
	if(strpos($wp->request, "mmr") === 0){
		// include get_template("layout-full-width");
		require_once( get_template_directory() . "/framework/functions/setup.php");

		$template = locate_template(array("template-layout-full-width.php"));
		include $template;

		die;
	}
}

This is the function I’m running on “parse_request” but I just get errors about undefined x_blahblah() functions… Starting with x_masthead_class()

Any idea what I need to do to make this work with the X theme?

Hello @jasonribeiro,

Thanks for writing in!

Please make use of the page.php because that will generate the X theme’s default page template. By the way, can you please post the complete error message so that we can check it? Meanwhile, in running your code, please ensure that the X theme is installed and active.

Hope this helps. Kindly let us know.

Hi Ruenel,

I tried adjusting the function to use that file. I tried both of the commented out lines in the below code

	public function virtualMmrPage(){
	global $wp;
	if(strpos($wp->request, "marketreport") === 0){
		// require_once(get_template_directory() . "/page.php");

		// x_get_view( x_get_stack(), 'wp', 'page' );

		echo "This is a custom page generated on the fly when visiting http://mysite.com/marketreport.";

		die;
	}
}

Both of these cause the same error though:

[06-Sep-2019 14:44:32 America/New_York] PHP Fatal error:  Uncaught Error: Call to undefined function     x_masthead_class() in /var/www/html/wp-content/themes/x-child/framework/views/renew/wp-header.php:15
Stack trace:
#0 /var/www/html/wp-content/themes/x/framework/functions/frontend/view-routing.php(182): include()
#1 /var/www/html/wp-content/themes/x/framework/functions/frontend/view-routing.php(55): X_View_Router::render('/var/www/html/w...', Array, true)
#2 /var/www/html/wp-content/themes/x-child/header.php(22): x_get_view('renew', 'wp', 'header')
#3 /var/www/html/wp-includes/template.php(722): require_once('/var/www/html/w...')
#4 /var/www/html/wp-includes/template.php(671): load_template('/var/www/html/w...', true)
#5 /var/www/html/wp-includes/general-template.php(41): locate_template(Array, true)
#6 /var/www/html/wp-content/themes/x/framework/views/renew/wp-page.php(9): get_header()
#7 /var/www/html/wp-content/themes/x/framework/functions/frontend/view-routing.php(182): include('/var/www/html/w...')
#8 /var/www/html/wp-content/themes/x/framework/functions/fronten in /var/www/html/wp-content/themes/x-child/framework/views/renew/wp-header.php on line 15

Hello @jasonribeiro,

It seems that the error is coming from a function in your child theme calling a function that does not exist in the child theme as well. Before anything else, please review our knowledge base articles about our Child theme and how to make template overrides and customizations in the child theme. Please check this out:

Hope this helps.

Hi Ruenel,

That function seems to be called in the legacy templates which is what our child theme is based off. It looks like it’s defined in this file in the original theme: /x/framework/legacy/cranium/headers/functions/classes.php

For the record, if I remove that function, I am able to get the page to load, but there’s no theme styling on the site. Upon closer inspection, it seems like it’s loading the theme files but this line:

x_get_view( x_get_stack(), 'wp', 'header' );

Is not including all of the right calls to stylesheets/scripts. Digging as much as I can… Looks like it’s possibly not connecting the child theme with the parent theme when loaded this way?

The child theme calls

x_get_view( 'global', '_header' );

But it’s failing to load the file /wp-content/themes/x/framework/views/header/base.php which should setup the theme assets

Hey Jason,

Based on what you said, it sounds like your child theme’s messed up or you’re not using the child theme that we provide. Our child themes were set up to inherit the parent theme’s styles, along with other essential connections with the parent theme.

To avoid confusion and maintain the discussion on pointing you to the correct template to modify, please backup your current child theme and delete it from your site. Please then download a fresh child theme from here: https://theme.co/apex/child-themes.

After that, copy page.php from the parent theme folder to your child theme and do the customization in the child theme.

I’d also like to point out that functions could not be overridden by copying the files in the functions folder like /x/framework/legacy/cranium/headers/functions.

Lastly, I’d like to let you know that we’ll do our best to lead you in the right direction. But, the nature of your question requires custom development and we could not be involved in that as it’s outside the scope of our product support.

Thanks.

Hi Christian,

Understood, and I appreciate all the efforts to help anyways.

I finally, for the most part, figured out what the missing piece was. In summary, in case anyone else is ever searching for a solution to this, I don’t think it’s related to our child theme as that works fine for every other piece of our site. But instead, this custom plugin code that I’m writing to setup the “virtual page” was apparently not triggering the template_include filter.

Idk if I need to tweak something to make that trigger happen, but for now I was able to get the results I wanted by calling

do_action('x_late_template_redirect');

At the top of the functions used to create the virtual page.

Again, thank yall for trying to help out!

Hey @jasonribeiro,

You are most welcome.
It’s good to know that you have figured it out a way to correct the said issue.

Best Regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.