how to make session login in wordpress

Viewed 817

How to add a session to the form that I made using a session from wordpress so that the form can only be accessed when the admin is logged in?

Thank you.

2 Answers

Why using a session to determine if an admin user is logged in?
You could simply use a build-in Wordpress function:

if(
    current_user_can( 'manage_options' )
) {
  // Current visitor is logged-in and has Admin capabilities ..
}

Wordpress Developer

Do it like

function init_sessions() 
{
   if (user_logged_on_site) {
      @session_start();
      $user_id = $_SESSION['wp_user_id']; 
      wp_set_auth_cookie( $user_id); 
  }
}
add_action('init', 'init_sessions');

$_SESSION is not used in Wordpress. so you need to add some code to your functions.php please check it from http://www.frank-verhoeven.com/using-session-in-wordpress/

Related