Search code examples
phpwordpresscode-snippets

wordpress code snippet php restricted pages access


I'm working on an existing wordpress project and i'm using ultimate member plugin. I think that to restrict access to pages if user is logged or not we have to pay for additional modules. So i tried php code snippets. I tried to use XYZ PHP code snippet but the snippet code is not working although it's active. I think i have to add the snippet created somewhere but don't know where plus the wordpress is in french. Can i go directly to a page edit and add snippet here is the code snippet :

<?php
if ( !is_user_logged_in() ) {
wp_redirect( 'https://xxxxir.com/register' ); 
  //auth_redirect();
}

xyz-ips snippet="redirection"

Thanks.


Solution

  • If you want to restrict specific page you can do it this way. Page that you want to restrict should have slug such as xxx.

    // Put these codes in function.php
    // xxx is slug of your page
    
    add_action('wp_head', function(){
    
        global $post;
    
        $post_slug = $post->post_name;
    
        if ( $post_slug == 'xxx' && !is_user_logged_in() ) {
          wp_redirect( 'https://xxxxir.com/register' ); 
        }
            
    });