Wordpress session management - wordpress

I'm putting up a site using Wordpress and I'd like to piggyback on its sessions. But I'm not finding any plugins, or even documentation. Any suggestions or references before I start hacking it?
Note: I'm asking about if and how WP uses standard PHP sessions itself, not how to add PHP sessions e.g. using session_start(). Apparently any state WP maintains is accomplished by other means. So if I want to use PHP sessions I need to add and maintain it myself entirely, using techniques like those in the thread.
Thanks all!

It's a very bad idea to modify WP Core files for the ability to use sessions. The best way I've found is to call the session_start() from init action hook.
function kana_init_session() {
session_start();
}
add_action('init', 'kana_init_session', 1);
You can place it in functions.php file of your theme.
Detailed article can be found here: http://www.kanasolution.com/2011/01/session-variable-in-wordpress/

WordPress doesn't appear to call session_start() because it wants to be stateless
and if register_globals is defined, it automatically destroys your $_SESSION

Consider using WordPress Transient API
Values stored using the Transient API are visible to all users, not just the current user, depending on the unique identifier used to retrieve the transient, you could assign each user a unique identifier essentially causing a transient to behave very much like a session.
Further considerations:
Depending on a users setup with object cache, etc., transients may
not always be stored in the DB (e.g. memcached), using transients for
sessions could mean that the data can get bulky and fill memory
quickly (in the use of memcached).
Also, it seems that WP does not do auto garbage collection for
transients:
https://wordpress.stackexchange.com/questions/6602/are-transients-garbage-collected

For what I need to do, the best answer involves:
To allow the cookie for wordpress to persist across subdomains, install the Root Cookie plugin.
sub1.domain.com has wordpress; sub2.domain.com is another site. From the other site (sub2), I read the cookies to identify who the user is and if the user is logged in.
My cookies are as follows:
[wordpress_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|d0249fced9c323835c5bf7e84ad3ffea
[wordpress_logged_in_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|56e9c19541ecb596a1fa0995da935700
Using PHP, I can loop over the cookies, parse the key=>value pairs. These cookies let me know that [mshaffer] has a cookie stored on wordpress, and also is authenticated as logged_in. The expiry of the cookie is 1255298821.
In sub2, I can query the database of wordpress and grab the user info:
SELECT * FROM `wp_users` WHERE user_login = 'mshaffer' ... grab user_id, user_email from this query
SELECT * FROM `wp_usermeta` WHERE user_id = '$user_id' ... grab lots of other data from wp
With this info, I can add to my sub2 session variable / cookie and do what I want with the data. I can identify if I am logged in, and my username ... which let's me grab lots of different data. I can now use WordPress authentication in my sub2.domain.com and redirect accordingly.
monte
{x:

Wordpress doesn't seem to use any sessions.
The best way to go about it is to use the action hooks it provides.

Have you checked the solution here this may work for here and its on easy way
http://thedigilife.com/wordpress-how-to-set-session-custom-variable-while-login/

Hooking a function with session_start() on wp_loaded seems to work in this case.

Put this code in wp-config.php at first line:
if (!session_id()) {
session_start();
}
Put this code in theme's header.php at first line:
session_start();
Then it will maintain all session variables.

If you wanna use your own session values, Wordpress does support it.
You need to add following lines at the top of wp-config.php
if (!session_id()) {
session_start();
}
Then add following line at the top of header.php
session_start();

Related

wp_delete_post() force deleting post, override ignored

I have set up a custom post type with a way to manage the posts on the front end of the site. On the front end users have the option to edit or delete posts.
When the user clicks delete, I run the wp_delete_post() function, to send the post to the trash. I don't want the post to bypass the trash, I would actually like the post to be sent to the trashcan.
The second parameter of the wp_delete_post is a boolean, true/false , which is supposed to dicate weather or not to bypass the trash.
The first is the ID of the post you'd like to delete.
http://codex.wordpress.org/Function_Reference/wp_delete_post
Here is my function:
wp_delete_post( $cpt_id, false );
As you can see, I've set the second parameter to false, but it still bypasses the trash and gets deleted from the server.
Is this a bug that I should report to core, or am I missing something simple?
Thanks!
Since you never want to delete and always want to trash, you could use wp_trash_post() instead. (http://codex.wordpress.org/Function_Reference/wp_trash_post)
However, that simply calls wp_delete_post() for you.
If none of your deletes are going to the trash, make sure that EMPTY_TRASH_DAYS is not set to 0 somewhere. (e.g. your wp-config.php) (http://codex.wordpress.org/Editing_wp-config.php)
HTH,
=C=

Session lost in wordpress pages

pirnt_r($_SESSION) not print all value.
But we log in wordpress admin and preview page when we write print_r($_SESSION) they display all session variable with value.
Thank you.
Can you show us the whole code?
Probable cause:
try print_r($_SESSION) not pirnt_r($_SESSION);
try to put echo before print_r($_SESSION); as in echo print_r($_SESSION);
Wordpress doesn't use session's to log their users in, so if you log in at /wp-admin there are no $_SESSION variables set. Wordpress also doesn't call session_start(). This means you have to start the session yourself if you haven't done so already.
You can do this in the following way:
if ( ! session_id() )
session_start();
If you know the session has not been started already you can use just session_start, like so:
session_start();
If you do this your $_SESSION array should contain the variables you put in it previously.
You can write your question here: https://wordpress.stackexchange.com/
maybe more helpful

Sitecore context in HttpModule

I created simple HttpModule, all what I need is just get is valid Sitecore.Context inside but during processing I see that my Sitecore.Context is some kind of default.
I can get Sitecore user that log in to Sitecore, etc.
How to fix it ?
I assume you have a Sitecore Context, but it is null or set to the wrong site or language.
You can change the context like this:
// switch to the preferred site
Sitecore.Context.SetActiveSite("yourSiteName");
// set the preferred database
Sitecore.Context.Database = Sitecore.Configuration.Factory.GetDatabase("master");
// set the preferred language
Language language = Sitecore.Globalization.Language.Parse("en");
Sitecore.Context.SetLanguage = (language, false);
You probably also want to switch back to the original settings after your processing is done. So it is wise to "save" the original settings in a variable so you can change them back afterwards
Take a look at John West's posts on Sitecore pipelines for some background on how the context is being established.
http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/05/All-About-Pipelines-in-the-Sitecore-ASPNET-CMS.aspx
and
http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/05/Important-Pipelines-in-the-Sitecore-ASPNET-CMS.aspx
The following blog post on creating and running custom pipelines should give you an idea how to implement your own pipeline.
http://adeneys.wordpress.com/2008/08/27/creating-and-running-custom-pipelines-in-sitecore/

Feeling bad about an overuse of get_option

I've been working with the settings API getting my head wrapped around it. Now that I feel I have a decent understanding of how to use it, I want to optimize how I'm doing it a bit.
My main concern is the use of get_option on every callback to create each of the settings inputs. I was planning on caching them so I didn't need to call get_option 10+ times.
My question is, before I do my caching, does wordpress have its own per-page load cache for get_option?
Thanks,
Max
No, WordPress doesn't have that kind of cache. It even calls get_option() too often.
EDIT #1: wp_load_alloption() will load every single options from database to cache, so get_option() will only load from database if none found on it's cache.
See below (the original answer) To reduce the use of get_option():
I prever to wrap them all to an object to reduce the usage of get_option. I've always do these first thing on my themes:
global $mytheme;
$mytheme = new stdClass;
$mytheme->something = get_option( 'something' );
I do for each setting that repeatedly used on my theme so get_option will only be loaded once.
Next I want the option to be call, I just use:
function something() {
global $mytheme;
$something = $mytheme->something;
return $something;
}
you can store multiple values in 1 option. Just feed update_option or add_option an array.
On the caching note take a look at these links:
http://www.catswhocode.com/blog/wordpress-transients-api-practical-examples
http://codex.wordpress.org/Transients_API

Check if user agreed to terms , set cookie

I'm a Drupal nub. I would like to check on every page if user (anonymouse) agreed to somekind of terms. I suppose i should write small custom module ?
Where will this condition be written
if(!$_COOKIE('confirm')){
//jQuery show confirmation form
//Set cookie for 1hour
}
maybee in page.tpl.php ? Please, give me some tips ..
If you don't want to store the info for a long time, you should use $_SESSION variable. Then in preprocess page you could check if the user has accepted and set a variable that you can use in your page.tpl.php.
user_save() accepts an array argument which you can put custom data into. This will then be loaded with your $user object and you can use in any template file.
Check out these modules:
http://drupal.org/project/legal
http://drupal.org/project/terms_of_use
The Legal and TOS modules are good if you need a login. If working with anonymous users, however, you'll need to use the rules module with https://www.drupal.org/project/rules_session_vars.

Resources