$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>functions - Restrict access if logged out except for homepage|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Restrict access if logged out except for homepage

matteradmin12PV0评论

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().

Post a comment

comment list (0)

  1. No comments so far