A plugin creates a set of pages and a template. There must be some method to assign the template to one of the pages. I've attempted to adapt the method shown at filter reference template_include
without success.
The page slug is member-register
, the template file name is member-content-template.php
.
Some of the things that don't work include:
locate_template()
: I suspect that because the template was created by a plugin it is not found by a function that appears to only search themes.is_page()
: If given the argument of the page's title, slug or id returns false.
Edit:
As I have little experience with WordPress, I work in small increments to see what yields results. Attempts include such things as:
$template = locate_template(['member-content-template.php']);
var_dump($template);die;
Which results in string(0) ""
Or:
$is = is_page(3350);
var_dump($is);die;
Which results in bool(false)
The same is also true for is_page('member-register')
and is_page('Register')
If any of the above had yielded acceptable results I would have built a callback as suggested in the filter reference.
Edit #2:
Code snippets:
rma.php (the plugin code)
$page_definitions = array(
...
'member-register' => array(
'title' => __('Register', 'rma-member-auth'),
'content' => '[custom_register_form]',
'class' => 'Rma\Pages\Register',
'function' => 'createRegisterForm',
'template' => 'Rma\Templates\member-content-template.php',
),
);
...
$templater = new PageTemplater($templates);
add_action('plugins_loaded', array('Rma\Templates\PageTemplater', 'get_instance'));
$pages = new PageLoader();
$pages->pageCreator($page_definitions);
$pages->shortcodeGenerator($page_definitions);
...
PageLoader:
...
public function pageCreator($page_definitions) {
foreach ($page_definitions as $slug => $page) {
// Check that the page doesn't exist already
$query = new \WP_Query('pagename=' . $slug);
if (!$query->have_posts()) {
// Add the page using the data from the array above
wp_insert_post(
array(
'post_content' => $page['content'],
'post_name' => $slug,
'post_title' => $page['title'],
'post_status' => 'publish',
'post_type' => 'page',
'ping_status' => 'closed',
'comment_status' => 'closed',
'post_template' => $page['template'],
)
);
}
}
}
template_include()
. Had the attempts been successful I would have built a filter based ontemplate_include()
.is_page
when the functions file is loaded, the query hasn't run yet for WP to know what page it is.