• Home
  • Forums
  • Contact
  • Log in (Lost Password?)
  • Register

Frank Verhoeven

Webdevelopment Blog

Forums

  • General Discussion
  • FV Community News
  • FV Code Highlighter

Recent Topics

  • FV Community News Alpha 2
  • FV Community News 3.0 Alpha Preview 1
  • word wrap / copy code
  • Adding a phone number to Community Events Form
  • Removing line numbers

Recent Comments

  • 12 WordPress Plugins to Display and Highlight Code within your Blog | Public Graphic Site on WordPress Plugin – FV Code Highlighter
  • Maxime Kieffer on Using Sessions in WordPress
  • 17 Useful WordPress Plugins That You Should Install On Your Own Blog « UShareDesign on WordPress Plugin – FV Community News
  • 10 个 WordPress 的代码语法高亮插件 | 61we.com on WordPress Plugin – FV Code Highlighter
  • nützliche neue und ältere WP Plugins on WordPress Plugin – FV Community News

Using Sessions in WordPress

Posted by Frank | Filed under PHP, WordPress

Yesterday, when I was creating a new plugin for my friend Stefan Vervoort, I needed sessions to work. Unfortunately, WordPress doesn’t support them?!

I searched the whole source code of this great piece of blogging software, and not on one single line I found even one session. Also the session_start() function is not called, but I still needed my sessions to work :-? .

So I started searching Google for a fix of my problem. I found a lot of people asking the same question: “Why do sessions not work in WordPress?” Finally I found a solution to fix this little issue and guess what, it is a simple one.

The Solution

The only thing you have to do is call session_start(); before any output is send to the client. Now you might think: Nice, but what happens when I upgrade my WordPress installation to the latest version? Well, yes, your changes will be lost. That is the reason why we first should think of where to add these changes..

Normally upgrading your installation will replace all files, except one. Yes, it is the wp-config.php file. And even better, there isn’t send any output to a client yet, when this file is loaded.

So, we add the next lines of code to our wp-config.php file:

1
2
3
4
5
6
7
8
9
/**
 * Enable sessions
 */
if (!session_id())
    session_start();

And sessions are enabled on your blog!
I think the best place to add these lines is at the top of the config file, so immediately after the php starting tag (<?php). This prevents errors in WordPress/Plugins/Themes from breaking it.

In your theme/plugin

As some of people have suggested in the comments, using the wp-config file might not always be the best solution.
Add the next piece of code to your functions.php or plugin file to enable sessions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * init_sessions()
 *
 * @uses session_id()
 * @uses session_start()
 */
function init_sessions() {
    if (!session_id()) {
        session_start();
    }
}
add_action('init', 'init_sessions');

I hope this will help you out when facing the same problem.

  • Share this:
September 20, 2008

100 thoughts on “Using Sessions in WordPress”

← Older Comments 1 … 3

← Older Comments 1 … 3

Leave a Reply Cancel reply

Please use the forums for support questions.

Your email address will not be published. Required fields are marked *

*

*



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Categories

  • Internet (1)
  • News (8)
  • PHP (4)
  • Web Hosting (1)
  • WordPress (3)
  • Wordpress Plugins (14)

Popular Posts

  • Wordpress Plugin - FV Community News
  • Using Sessions in WordPress
  • Wordpress Plugin - FV Code Highlighter
  • Short If-Else Structure

Tags

Community News FV Community News Hosting Internet News PHP Web WordPress Wordpress Plugins

On The Next Page

  • FV Community News 2.0 Public Beta - The beta has been around for a while…
  • Beta Testers Wanted - I’m happy to announce that the Community News…
  • New Website Design - After almost one year of inactivity, I’m back!…
  • New JavaScript Used - Hey everyone. After adding version 1.3, I received…
  • Fv Community News Version 1.3 - After more then 1000 downloads, and a few…
  • FV Community News V1.2 - Finally, after a lot of programming the time…

Copyright © 2012 Frank Verhoeven

Home | Sitemap | Top