0

I have a HTML page which I wanna use inside a WordPress website. But this page should be only available to users who logged-in and non logged-in users should be redirected to login page. Since I need to check user status a PHP page should do the job and if user is logged-in it returns the HTML content.

is_user_logged_in function would do the job but I'm confusing how to implement it since I far as I read on plugin development my case doesn't fit neither in Actions nor Filters. Also do I need to place my own PHP page there or this can be done via a built-in WordPress page? If both, which one is recommended?

Note that the page address is always the same for all users.

  • How are you outputting your content in that page? I'm not sure what do you mean by HTML page since WordPress is PHP based. – Jack Johansson Apr 5 '17 at 23:00
  • For sure I will return HTML content from a PHP page which is gonna be a built-in WorPress page or my own PHP page. – Xaqron Apr 5 '17 at 23:01
  • Well then it would be easy to achieve what you want. Use is_user_logged_in to check for members, and then wp_safe_redirect afterward to redirect non-logged in users to login page. – Jack Johansson Apr 5 '17 at 23:05
  • The question is how to embed the PHP page into WordPress. Simply using that functions doesn't work and for making it a Plugin this is not what usually Actions and Filters' do. Any Plugin` sample with similar scenario is appreciated. – Xaqron Apr 5 '17 at 23:09
1

You can create a page after your plugin is activated. Take a look at this:

register_activation_hook( __FILE__, 'insert_page' );

function insert_page(){
    $my_page = array(
      'post_title'    => 'My Page',
      'post_name'     => 'MyPage',
      'post_content'  => 'My page's content.',
      'post_status'   => 'publish',
      'post_author'   => get_current_user_id(),
      'post_type'     => 'page',
    );
    wp_insert_post( $my_page, '' );
}

Then assign your template file to your custom page:

add_filter( 'page_template', 'my_custom_page' );
function my_custom_page( $page_template )
{
    if ( is_page( 'MyPage' ) ) {
        $page_template = plugin_dir_path( __FILE__ ) . '/MyPage.php';
    }
    return $page_template;
}

Now, you have your page and your template. In your template, use this code to detect the visitor's status and decide to redirect them or not:

<?php 
    if (!is_user_logged_in()) { 
        wp_safe_redirect( site_url('/wp-login.php')); 
        exit;
    } 
?>

After this code you can safely output your content, since the user will be redirected before outputting the content if he/she is not logged in.

Let me know if you need further assistance.

  • Thanks. Then where to set the page unique address (URL)? I want the website works as before installing my Plugin and just the new page be added to it. – Xaqron Apr 5 '17 at 23:31
  • You are welcome. post_name is the slug of your page, so your page's URL will be like http://example.com/MyPage. But i don't understand what do you mean by added to it. If your plugin is not installed, then the page will not be created though. – Jack Johansson Apr 5 '17 at 23:34
  • I meant no other modifications to the rest of website which seems will not happen with your solution. – Xaqron Apr 5 '17 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.