0

I'm successfully utilizing a child theme in Wordpress. I would like to create a page template that simply forces login for any visits to the pages where this template is in use. (Simply need to add <?php if (!is_user_logged_in()) { auth_redirect(); } ?> to a separate template.) Copying the existing template down to the child theme makes updating tedious and prone to failure for such a simple call. However, I cannot find a way to create a new template that calls a parent template. I will admit that I may not be understanding an existing call, but I have been unable to see any way to do this.

Setting URL filters for every page is not a usable option. I'm also unable to use slugs, as they are already in use for another purpose.

  • why not save a flag in post meta instead, and remove the need to couple it with a template? you can use @userabuser's solution by checking post meta instead of page template. – Milo Mar 1 '16 at 19:04
  • Primarily because these are pages and do not include posts in the traditional sense. It seems that you can't set Categories and Tags for pages. – G H Mar 1 '16 at 19:34
  • you technically can add categories and tags to pages, however, I'm talking about post metadata, aka Custom Fields, which can be added to any post type. – Milo Mar 1 '16 at 19:57
  • Can you put this an answer so that I can mark it answered? It may not answer the question directly, as there still isn't a new template that inherits a parent template, but it does perfectly solve the problem. I'll edit the answer with the correct function code once it's in place, as I can't properly place it here. – G H Mar 1 '16 at 20:27
0

Try...

function maybe_auth_redirect() {

    if ( is_page_template('my-template.php') && ! is_user_logged_in() ) {
       auth_redirect();
    }

}

add_action('template_redirect', 'maybe_auth_redirect');
  • This looks like something I would put in functions.php, not the creation of a new template. One of us missed something here. I need to create a template that I can assign specific pages to. ONLY those pages should be checking to see if the user is logged on. I DO NOT want to have to, say, copy page.php down to the child every time the parent theme updates. – G H Mar 1 '16 at 18:34
  • Put this code in your functions.php file... If any page is using the template "my-template.php" and the user is not logged in, they will be redirected to login. Otherwise you can use ! is_user_logged_in() and auth_redirect() within the template directly, but's that not recommended. If so, you need to call it before printing headers/output. E.g. get_header() – Adam Mar 1 '16 at 18:39
  • So, what's in my-template.php? Surely it can't only have a /*template name: my-template.php */ line can it? – G H Mar 1 '16 at 18:53
  • I see part of the issue causing the misunderstanding, there was code in the question that simply disappeared when posted. – G H Mar 1 '16 at 19:00
  • So, what's in my-template.php? <-- that's your template, it can have whatever you want in there. You said you are assigning a template to a page, if so, then that template should have structure, e.g. get_header(), get_footer() etc... The code above goes into your functions.php file, when it recognizes a page that is using your desired template file, it will run the logic shown above first, before outputing the template to screen, if the user is logged out, it will redirect them as you desire. – Adam Mar 1 '16 at 20:01
0

I solved the issue at hand by creating a Custom Field of requires_login and setting it to "true", then adding the following code to functions.php:

function require_login() {

    if ( get_post_meta(get_the_ID(), requires_login, true)  && ! is_user_logged_in() ) {
       auth_redirect();
    }

}

add_action('template_redirect', 'require_login');

Marking this as answered, as it solved the problem. However, if someone were to show how to have an inherited template, I'd consider changing the answer. Many thanks to @Milo for pointing me in the right direction. Apologies to @userabuser for the unintentional disappearing code trick.

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.