I am new to WordPress, i want to implement custom Login functionality using WordPress. I am using the following Code to create custom login,

In wp.config file

if( !session_id() )

In header.php i am using the following code:


and after fetching data to create session used the below code:

 $_SESSION['myKey'] = $row['ID']; 
 header("location: http://localhost/newwordpress/?page_id=2");

in function.php i have created logout function as below:

function example_logout() {

if (session_id() ) {
add_action( 'wp_logout', 'example_logout' );

everything is working, when i refresh the page or move to any other page, session gets destroyed, please help me how can i maintain session using WordPress.

I have asked the same question on stackoverflow then someone tell me to ask here.. so i am just copying my question here.... please help me..


I have tried the php_wordpress_and_session, but still i am not able to maintain session on the page.

  • 3
    Before going any further could you please clarify why you want to do this in plain PHP and bypass WordPress Login/Session functionality. Also what is the end-goal you try to achieve? – kraftner Feb 18 '14 at 16:33
  • @kraftner please see updated question – raman Feb 18 '14 at 17:47
  • Seeing as you paid no attention to the last two people who said it, I thought I'd become the third person to say: There is no need for you bypass Wordpress' session functionality; and it is inadvisable. – Bill Feb 18 '14 at 18:19
  • Your updated question does not address the concerns of the three voices who spoke out against doing this: there's a better way, and I promise you PHP sessions aren't the way. – Bendoh Feb 21 '14 at 17:47

First answer this question: Why bypass the WordPress login/auth system? What's the end-game here?

I would highly recommend against using PHP sessions to maintain user state in the WordPress world. More likely than not, a cookie will be sufficient.

For using cookies, you can use the PHP setcookie() function to set cookies, and the $_COOKIE superglobal to access cookies.

However, cookies aren't great for all purposes, especially cases where sensitive or internal information needs to be stored. In general, any purposes where you just need to save simple user preferences or user state containing otherwise-exposed information (like post IDs,) cookies can be very useful.

  • please see updated question – raman Feb 18 '14 at 17:47

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.