restrict_access_if_logged_out function except for homepage

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_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;

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’) ){
add_action( ‘wp’, ‘restrict_access_if_logged_out’, 3 );

Read more here:: restrict_access_if_logged_out function except for homepage

Leave a Reply

Your email address will not be published. Required fields are marked *