functions - Restrict access if logged out except for homepage

admin2025-06-06  7

I want to retrict access to my whole wordpress except for the homepage and one other page. The problem I found is that the other page is accessible but not the homepage.

My wordpress installation is behind an nginx reverse proxy

location /wordpress/ {
    proxy_pass              http://192.168.1.12/;
    proxy_set_header      X-Forwarded-For $remote_addr;
    proxy_set_header      X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-Proto       $scheme;
}

On another nginx server

server {
listen       80 default_server;
server_name  192.168.1.12;

root /var/www/html/wordpress;
index index.php index.html index.htm;

access_log      /var/log/nginx/default.access.log;
error_log       /var/log/nginx/default.error.log;

## WordPress Perm links config ##
location / {
try_files $uri $uri/ /index.php?$args;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

The wordpress wp-config.php was modified for ssl

if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) ||
     (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) {
    $_SERVER['HTTPS'] = 'on';
}

The theme's function.php was modified to restrict access

function restrict_access_if_logged_out(){
  global $wp;
  if (!is_user_logged_in() && !is_home() &&  ($wp->query_vars['pagename'] != 'portail-identification') && ($wp->query_vars['pagename'] != 'portail-stagiaire') ){
       wp_safe_redirect(wp_login_url(get_permalink()));
       exit;
  }
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );

Also created same question on

I want to retrict access to my whole wordpress except for the homepage and one other page. The problem I found is that the other page is accessible but not the homepage.

My wordpress installation is behind an nginx reverse proxy

location /wordpress/ {
    proxy_pass              http://192.168.1.12/;
    proxy_set_header      X-Forwarded-For $remote_addr;
    proxy_set_header      X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-Proto       $scheme;
}

On another nginx server

server {
listen       80 default_server;
server_name  192.168.1.12;

root /var/www/html/wordpress;
index index.php index.html index.htm;

access_log      /var/log/nginx/default.access.log;
error_log       /var/log/nginx/default.error.log;

## WordPress Perm links config ##
location / {
try_files $uri $uri/ /index.php?$args;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}

The wordpress wp-config.php was modified for ssl

if ( (!empty( $_SERVER['HTTP_X_FORWARDED_HOST'])) ||
     (!empty( $_SERVER['HTTP_X_FORWARDED_FOR'])) ) {
    $_SERVER['HTTPS'] = 'on';
}

The theme's function.php was modified to restrict access

function restrict_access_if_logged_out(){
  global $wp;
  if (!is_user_logged_in() && !is_home() &&  ($wp->query_vars['pagename'] != 'portail-identification') && ($wp->query_vars['pagename'] != 'portail-stagiaire') ){
       wp_safe_redirect(wp_login_url(get_permalink()));
       exit;
  }
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );

Also created same question on https://stackoverflow/questions/53157349/restrict-access-if-logged-out-except-for-wordpress-homepage

Share Improve this question edited Nov 5, 2018 at 15:29 Carobell asked Oct 30, 2018 at 19:49 CarobellCarobell 1431 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Is your "home page" displaying a list of the site's blog posts, or is it set to display a specific page? If the latter, you need to use is_front_page() instead of is_home().

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749204249a317235.html

最新回复(0)