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]
- Restricting Image Upload Sizes using ‘wp_handle_upload_prefilter’ – Stuck media progress bar when Featured Image?
- How to keep track of when a vote is cast?
- trouble with page_id & highlighting current page
- Convert the_permalink() function output into string
- Making my first wordpress template
- How to use array in function to get only value I want
- Apply class to every third list item? [duplicate]
- Only show image from custom field when present
- Cinema site, retrive the movies that actor play in
- retain querystring values when savincustom options in admin
- PHP if statement works in template files, but not header
- How Can I Edit the Registration Code?
- Error while setting role
- Editing a PHP file to include a small text piece on every page
- Get categories names as an array to use it in theme settings
- Should i use the wordpress Options table or to create database table..?
- variable value disappears in a second loop
- Force Users To Relogin
- unknown issue in ‘header.php’ preventing theme from displaying fully
- Hide a div to subscribers(user role) in one page (is_page)
- Security updates to 3.3.2
- Where is this function’s callback getting its arguments from?
- How can I use a sliced image As banner? [closed]
- persist a variable set in header.php all the way down to footer.php
- Echo title attribute php
- Can a page contain php code?
- Last class on last headline?
- Unable to Find Space in Custom Function
- Is it possible to display a DIV to not logged in users and then change it for users who are logged in?
- Strip Paragraph Tag
- Unable to process php via shortcode
- working with term_relationships table
- Creating a blog on my companies new website using WordPress but I have sidebar issues
- Creating a mobile site by adding a new class to the body tag from functions.php
- WordPress plug-in for dynamic download link?
- Using WP functions such as the_title() in an included php file
- GET problem in WordPress
- WordPress gallery shortag
- Almost Done… Post Format Code
- wordpress admin plugin menu custom css
- Help with page nav?
- Show recent posts starting at a specific number archive
- Add title=”” to A PHP Code
- Get Page ID from Backend
- Help with WordPress function inside a shortcode
- Wpdb->insert() doesn’t insert new row after the last one
- WordPress plugin admin page – using WordPress function in linked php file
- HTML table attributes ignored
- What’s the best way to include PHP code in pages?
- Is there any wordpress plugin to upload images and captions to multiple pages once
- How to fix this PHP warning in WP-Admin after upgrading to 3.1.2?
- PHP in Edit Post link
- Removing the first 8-10 letters from a post?
- How to add drag and drop functionalities to a div inside option panel
- WordPress and MySQL: trying to print data using PHP from user_meta custom field data
- Timthumb.php image gallery not working on Multisite WordPress
- Function Reference Documenting Template Tags for use in Custom Theme Templates?
- How do you create two separate Register pages?
- $_GET[‘updated’] does not work in WordPress 3.1?
- Why doesn’t PhotoSmash plugin play well with wp_query?
- How to send form data from WordPress (Meta Box) to an OData service?
- How to Configure Events List in WordPress to Disappear Event Once Date is Past
- Removing WordPress Footer -without access to PHP code?
- dynamic page using php from sql database
- Upload non-featured image to image field
- Usage of call back function of add_meta_box()