I wanted to restrict my website except a specific page. This is my solution:
function restrict_access_if_logged_out(){
global $wp;
if (!is_user_logged_in() && !is_home() && ($wp->query_vars['pagename'] != 'name-of-page') ){
$redirect = home_url() . '/wp-login.php?redirect_to=' . esc_url($_SERVER["HTTP_HOST"] . urlencode($_SERVER["REQUEST_URI"]));
wp_redirect( $redirect );
exit;
}
}
Only I added this: && ($wp->query_vars['pagename'] != 'name-of-page')
in IF clause and a global variable $wp
for exclude a specific page. This method avoid redirect when the pages is ‘name-of-page’.
My inspiration here.
And thanks @PieterGoosen for your solution in your post. 🙂
Related Posts:
- Does the debug.log do log rotation?
- the_post_thumbnail() based on the Post ID
- Add Adsense code in index.php
- How to remove this function from the homepage?
- How to display recent posts on home page with custom HTML
- How can I hide Home in my menu in a specific page?
- Preferred Method of debugging a wordpress SQL calls?
- Same log message keeps on printing to debug.log file thousand of times
- Displaying the last post on static homepage
- Let Users Choose Post Categories
- two columns of posts on homepage, one of them “favorites”
- Auto 301 to full post permalink? (using : /archive/%post_id% to %post_name% )
- Pagination broken on is_front_page()
- How to include custom code on a page that’s set to act as homepage?
- Amend theme php to include certain category in header
- show recent posts php code error?
- Change homepage content if user is logged in – BuddyPress
- Is it possible to restrict files from your wordpress uploads (not logged-in users or guest)?
- page 1 is not paged
- Link to index.php from home.php?
- Menu not updating for logged in users after redirect
- Having trouble with customizing blog posts on the home page
- image on homepage disappeared due to change of permalink name
- How to block access to a folder inside of wp-content for non logged in users?
- How to send logs to plugin owner for a plugin?
- Different home page for logged off users
- Insert div after every three posts in home.php [duplicate]
- How to display html code on homepage
- Use page.php for front page
- How to hide something from custom home page with php code?
- Adding if statement to content for homepage
- I want Page titles and excerpts to show up on home.php in certain order
- Adding slides to an existing carousel manually
- Not able to see the error [closed]
- How to create custom 401, 403 and 500 error pages?
- Why have on every line
- add_action in namespace not working
- Variables declared in header not available in other includes
- Failed to open stream / no such file or directory
- Need to get specific data from array
- Use different javascript files for each page on website
- WooCommerce – Fixed quantity of a product [closed]
- How to create post thumbnail
- Settings API – sanitize_callback is not called and it leads to an incorrect behavior
- How to get the POST TITLE using the POST ID?
- Importing hard coded custom field into acf field
- Adding country tags automatically
- From where the header-text can be changed in WordPress custom header?
- Trying to establish connection to External Database
- Load a page into a div with Ajax
- Login/logout in header
- Applying A Category to Existing Posts Where Page Title Matches Regex
- Dashboard broken into list of links
- Displaying recent posts on static page with template-part via shortcode
- My custom get_the_excerpt() can’t get excerpt by ID
- WordPress plugin: render only page content using ajax call
- How to fix this warning:call_user_func_array() expects exactly 2 parameters, 1 given in D:\wamp\www\…….\wp-includes\class-wp-hook.php on line 286
- My php code is not being executed
- Using Shortcode to Grab Archive Listing, Separate by Year
- Function won’t run onclick using Ajax
- working with term_relationships table
- Creating a blog on my companies new website using WordPress but I have sidebar issues
- How to show meta value code HTML after x paragraph
- List all blogs, but exclude the main site
- Check class_exists before class definition / Doxygen problem
- How do I create a secondary version of the_content
- url_to_postid returns 0
- How to Fix an Archive.php That Displays All Posts?
- Dynamically replicate custom metabox in custom post type
- How to tell if a user has gone in and created a menu
- Change Query Arguments (filter) with jQuery/Ajax or PHP?
- Tracking Visitor LatLng with WordPress using JS, PHP. How to put data which was extract using JS into DB
- php script in elementor
- WP-Bakery – Add custom colors to buttons with template colors
- WordPress redirect to a subpage – how to create a template for subpage?
- Trim Titles Only On Some Pages
- Commenting requires wordpress login, wordpress discussion and post comments tick boxes are checked
- can I extend the WP_Query class to deal with ‘duplicate’ posts created by joining to wp_posts?
- Enqueue sripts and styles only if function is called
- Return author name as a tag using get_author
- How to pull date/time in french format for wordpress post?
- WooCommerce subcategories help
- Echo Option Value Based On WordPress User Role
- Migrating a live WordPress website to a Local Server: why some elements of the website are missing?
- Store stripe info as user_meta
- Is there a way to have chunks of a page’s content expire?
- Loop returns more items than exist?
- Formatting Shortcode to Display in Visual Mode
- wp_nav_menu doesn’t work in a duplicate server
- Rewriting to a different page based on existence of query string?
- How to get category pages to look like a certain archive page?
- I am extending Walker_Nav_Menu: How can I get the number of children elements of the current item?
- How to call a specific value within the functions.php file?
- How to define template directory in this widget code
- WordPress plugin/code to to to next/previous post in same category
- Display total count of products in orders of a specific order status
- Add/alter image attribute before render
- How do I do so that people can register on my wordpress site?
- How can I improve and optimise my wordpress web server for better performance in 2023
- Internal server error when enabling Multisite in WordPress