• Hi All,
    I am now using a theme that uses header images to generate a nice background image.

    What I would like ot do though is have the randomize options turned on, but if a user visits that they get the same header image for that session, to save it changing on page navigation etc.

    Is this possible? If so how would I do this?

    Drogo

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You’re using a theme not found in the WordPress repository, so please use their official support channel. We feel they are best equipped to support their products.

    https://github.com/cogdog/wp-dimension/issues

    Thread Starter DrogoNevets

    (@drogonevets)

    I have modified the theme to my own ends.

    this is a question around the header image functionality that comes within wordpress not the theme.

    the function call to set the image is <?php header_image(); ?>

    What I am after is getting that to return the same value per session, if it requires an override of the function that is fine, just wondering if this is built in functionality/best place to add it, etc

    Drogo

    Thread Starter DrogoNevets

    (@drogonevets)

    I’ve created my own function to do it the themes functions.php

    Can someone tell me if I have done it in a really awful way etc please?

    function get_session_header_image() {
        if(!session_id()) {
            session_start();
        }
    	
        if(!ISSET($_SESSION['header_image'])) {
    	$_SESSION['header_image'] = get_header_image();
        }
    	
        echo $_SESSION['header_image'];
    }
    • This reply was modified 6 years, 2 months ago by DrogoNevets.
    Moderator bcworkz

    (@bcworkz)

    Presumably you call this in place of header_image() on the template? I’m 99% sure the answer is yes. Your code is fine.

    Thread Starter DrogoNevets

    (@drogonevets)

    That is correct. Just making sure tehre wasn’t a built in function somewhere for it.

    Thanks

    Moderator bcworkz

    (@bcworkz)

    Nah, WP doesn’t do sessions by default, that would be plugin or theme territory. Sessions are frowned upon for items in the .org repository, but of course you are free to do so on your own site.

    Thread Starter DrogoNevets

    (@drogonevets)

    Nah, WP doesn’t do sessions by default

    But we get qa cookie called wordpress_810f47eaf36d5ac275fd99257b16f899 which presumably is the WP session?

    I was hoping to not have to create a session manually

    Thread Starter DrogoNevets

    (@drogonevets)

    Reading more into this, what are people’s thoughts on this solution?

    Moderator bcworkz

    (@bcworkz)

    I’m not so sure global $wp_session is valid any more. Unverified. WP does have a WP_Session_Token class, but AFAIK it just saves cookies. It’s not the same as PHP sessions which only save in cookies for particular server configurations. It’s the same concept, but implemented differently.

    WP internals are constantly evolving and it can be difficult to keep up on all changes. Things outwardly affecting users are of course announced. Internals are hit and miss.

    Maybe I should rephrase and clarify. Use of PHP sessions are frowned upon in .org repository code because we cannot know how servers save sessions. Saving user data in cookies by whatever means is perfectly fine.

    Thread Starter DrogoNevets

    (@drogonevets)

    That’s fair thank you.

    Whilst I am an experienced web dev, i am more JS (Full stack) rather than PHP so quite new to WP.

    I have settled for my solution, minus the starting of the session, which i have done for me using a plugin I found that is somewhat more complex and handles options better (not that I need it to for my [i]very[/li] simple use case)

    Thanks again everyone!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Header image by session’ is closed to new replies.