Making a wordpress plugin accessible to logged in users only?

Viewed 26

I am trying to add code to my wordpress plugins index.php, so that only logged in users will be able to access it.

this plugin shows information from database which should be available for logged in users only.

I add the code below at top of index.php of plugin:

add_action('init', function() {

  if (function_exists('is_user_logged_in')){
    if (is_user_logged_in()) {
        
        if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

}
else
{
echo "user is not logged in.";
exit();

}
}
});

But this code is making whole website inaccessible to users who are not logged in.

I identified problem is with action hook init. I tried with hook plugins_loaded also but the result is the same.

it is crucial that I check if user is logged in or not because I am storing data for the logged user in database.

Can anyone help me please?

thanks

0 Answers
Related