Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

A minimal example of my problem would be

  • default background colour is white
  • a custom sidebar widget offers to switch background colour to green
  • when switched to green, background shall stay green while exploring the blog

Is there a standard interface in Wordpress for saving a global PHP variable $bgcolor to the viewer and read it from him anytime he browses to another blog page.

(I'd like to avoid setting a cookie "by hand")

share|improve this question
1  
Why do you like to avoid setting a cookie? Saving data for the user's session is made for a $_SESSION cookie. –  Mike Madern Jun 10 '13 at 12:52
    
@MikeMadern Seems solid. I was so stubbornly looking for a WordPress way I ignored PHP standards ... –  arney Jun 10 '13 at 14:08
    
And here is a HowTo –  arney Jun 10 '13 at 14:26
add comment

1 Answer

up vote 1 down vote accepted

Is there a standard interface in Wordpress for saving a global PHP variable $bgcolor to the user and read it from him anytime he browses to another blog page.

There is nothing that will automagically do it for you but it sounds like you just need to save a little bit of user meta, and of course retrieve it.

Since you asked about saving the variable, I am going to assume that the custom sidebar widget that switches the background color already works. Just add code, based on the two Codex entries above, that checks the user meta and saves changes.

If you post your code, I may be able to give a more elaborate answer.

If you are talking about generic visitors, and not WordPress users, setting a cookie is your best choice. There are no built in generic "cookie setter" functions that I am aware of. WordPress itself uses PHP's setcookie in a fair few places, for example in wp_set_auth_cookie, and I don't know why that function would need a "helper". It is fairly straighforward.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.