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.


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']);

Which results in string(0) "" Or:

   $is = is_page(3350);

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();


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
                        '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'],
  • Please show us what you have tried. – Milo May 11 '17 at 15:25
  • @Milo Various attempts included in edit. – geoB May 11 '17 at 16:06
  • did you try template_include()? – rudtek May 11 '17 at 16:17
  • @rudtek The attempts shown above were derived from the documentation for template_include(). Had the attempts been successful I would have built a filter based on template_include(). – geoB May 11 '17 at 16:22
  • The important part is when you do things. If you're checking is_page when the functions file is loaded, the query hasn't run yet for WP to know what page it is. – Milo May 11 '17 at 16:24

locate_template function searches your current theme's folder for template name(s). If your templates are in a root folder of your theme, use:

$template = locate_template( 'member-content-template.php' );

otherwise, precede the name of a template with subfolder's name ( no slash in front ):

$template = locate_template( 'templates/member-content-template.php' );

To load a template ( if found ) add true as a second parameter of locate_template function:

$template = locate_template( 'member-content-template.php', true );

Update: To load a tempate in your plugin, use filter 'template_include':

function my_template_loader_filter() {
    function my_template_loader( $template ) {
        $my_template = locate_template( 'member-content-template.php.php' );
        if ( ( '' != $my_template ) && is_page( 'member-register' ) )
            return $my_template ;
        return $template;
    add_filter( 'template_include', 'my_template_loader' );
add_action( 'after_setup_theme', 'my_template_loader_filter' );
  • As mentioned in the question, the template is created in a plugin, so I can only assume for now that locate_template() may return a template. Nor does this indicate how to assign such a template should one be returned to a page also created in the same plugin. – geoB May 11 '17 at 22:16
  • Check 'Update', for how to load a template in your plugin. – Frank P. Walentynowicz May 11 '17 at 22:41
  • The plugin currently loads a template; it cannot yet assign that to a page. I'll update my question with code snippets show template & page creation. – geoB May 11 '17 at 23:48

As I seem to have just learned, a page created in a plugin is not considered a page for purposes of adding a template. Instead, I found that the following additions worked:


$id = get_page_by_title('Register')->ID;

and, in PageLoader:

public function updateRegisterTemplate($id) {
    $meta_key = '_wp_page_template';
    $meta_value = './member-content-template.php';

    return update_post_meta($id, $meta_key, $meta_value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.