Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Show a custom template, no matter the page being viewed, if the user is not logged in

This turned out to be far easier than I had feared, using the template_include filter –

/**
 * Override the standard WordPress template with the 'please login' template if the current user is not logged in
 */
add_filter('template_include', 'portfolio_page_template', 99);
function portfolio_page_template($template){

    /** List the pages that are authorised for non-logged in users */
    $authorised_pages = array(
        get_site_url().'/login/',
        get_site_url().'/wp-login.php'
    );

    /** Ensure that the user is actually NOT logged in */
    if(!is_user_logged_in()) :

        /** Ensure we are not trying to view an $authorised page (to avoid a loop) */
        if(!in_array($url, $authorised_pages)) :

            /** Setup the new template */
            $new_template = locate_template(array('login-page.php'));
            if($new_template !== '') :
                return $new_template ;
            endif;

        endif;

    endif;

    return $template;

}

Related Posts:

  1. Loading partial templates with AJAX/PJAX
  2. Custom reset password template after forgot password request
  3. How can I modify the default reset (lost) password email text?
  4. Templates & CSS – Proper Programming Practice?
  5. index.php template is used instead of blog page
  6. Load template file without a post type
  7. How to apply a custom template for posts by default?
  8. Unable to include a template
  9. Login WordPress website using wp-rest api
  10. What should I put on my index.php?
  11. When a link doesn’t exist, how to tell WordPress what to do? (404 Error Page)
  12. Is there a way to set different post templates for parent posts and child posts in the same post type?
  13. Login page theming?
  14. Having trouble with Template hierarchy. I Need to create a set of pages that drill down from states to specific locations
  15. Custom templates vs page-slug
  16. Understanding the Template hierarchy
  17. How does WordPress choose archive type template?
  18. Use one template only for custom post type archives?
  19. get_month_link uses what wordpress template?
  20. Differentiation between index.php and page.php
  21. Single-page.php template file name not shown
  22. Can I assign a Folder for Post Formats, without it affecting WordPress’ fallback/hierarchical system?
  23. How to display custom post type?
  24. Assigning custom page templates to a static blog page
  25. Archive-custome_post.php template not working
  26. Organize template parts and page templates in folders in regards of template hierarchy
  27. Help Understanding Template Hierarchy
  28. User role templates
  29. Assign template to custom page type?
  30. BuddyPress : how can I call the template WordPress would have chosen (template hierarchy)?
  31. How can I make custom page templates work on child theme?
  32. Which template(s) to override to use a different sidebar for blog, single posts, categories, blog archive?
  33. Single Post Templates Doubt
  34. Page Templates Used in Custom Post Type
  35. Unload templates; disable parent Template Parts using only “theme.json”
  36. How to use Class in Java?
  37. Get name of the current template file
  38. How do you find out which template page is serving the current page?
  39. How to change the default registration email ? (plugin and/or non-plugin)
  40. How do I get the size of an attachment file?
  41. Is there any way to use get_template_part() with folders?
  42. single-{$post_type}-{slug}.php for custom post types
  43. Adding Page Attributes Metabox and Page Templates to the Posts Edit Page?
  44. How can i change email template for new user
  45. Any advantage to using get_header() over include(‘header.php’)?
  46. Enable page templates. How?
  47. Is it possible to override the result of get_template_part()?
  48. Use [embed] filter in template files
  49. What’s the purpose of the paged.php file?
  50. How do you access the Product Short Description in a WooCommerce email template? [closed]
  51. Prevent comments_template() to load comments.php
  52. Is there a way to list all used/unused WP templates?
  53. Page editor missing Templates drop down
  54. Print WordPress template filename(s) for debugging
  55. How to force TinyMCE in WordPress to replace newlines with tags and not with  
  56. How to find the posts page (home page) programatically
  57. WooCommerce: Change template for single product page
  58. View WordPress page template usage (or unused)
  59. How to edit contents of dynamic_sidebar()?
  60. How to check if a shortcode exists?
  61. Where is get_header(‘shop’) file in Woocommerce templates
  62. Custom templates folder
  63. What is the link to my default “archive” page?
  64. How to get slug of current category in taxonomy template?
  65. How to show a under construction page for a domain but still be able to work on index.php?
  66. Change the name of the ‘Default Template’
  67. Customizing get_the_excerpt() to specific length and “Read More” output.
  68. How to Rename a Template File?
  69. Template for specific post of custom post type
  70. get page templates
  71. Echoing Elementor page content in template, but it doesn’t get styles and some widgets are missing
  72. Using WordPress templating for HTML emails
  73. Is it possible to manipulate the list of page templates?
  74. WP frontend output of custom textarea fields not respecting line breaks. In admin it’s OK
  75. How do you check if a WordPress template file exist?
  76. Multiple Single Post templates
  77. Taxonomy Templates… by hierarchical level?
  78. Load entire NextGEN gallery from single thumbnail?
  79. I put my blog on a subpage, how do I get page title?
  80. A special single page templates for posts under a category and all its child category
  81. Outputting Canonical Resource URLs Across a Multisite Network?
  82. Gravity Forms Custom Templates [closed]
  83. Child Theme: how to override variables?
  84. How do I get the URL of a specific size featured image?
  85. Where to store some per-template preferences?
  86. add/apend templates ‘transparently’ via plugin to currently active theme or child theme?
  87. stop blog page from using index.php?
  88. Different templates for parent and children categories/taxonomies
  89. How to create tag.php
  90. Template files missing after moving site
  91. How do I override template-tags.php in twentyseventeen theme
  92. Get template part vs locate template function
  93. Programmatically change post templates?
  94. How to hide/redirect the author page
  95. Tools for Converting an Existing Website Design to a WordPress Template?
  96. Gutenberg & Pre-formatted Templates: Core Block Attributes
  97. Restrict a search to a custom post type
  98. Gutenberg Editor – Template Doesn’t Match Error
  99. author.php added to template not working on Nginx server
  100. Test if page is child and has children, if so echo child pages also on grandchild pages
Categories templates Tags login, template-hierarchy, templates
Sanitize html, where to sanitize
How do I deliver posts based on user geolocation

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress