简体   繁体   中英

Set and retrieve the session values wordpress

I'm using WooCommerce. I set my own session on page.php, and trying to pass that session to mini-cart.php, but mini-cart isn't receiving that session.

page.php is like this

get_header("english"); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
    <?php
    // Start the loop.
    // session_start();
    while ( have_posts() ) : the_post();

        // Include the page content template.
        get_template_part( 'template-parts/content', 'page' );

        // If comments are open or we have at least one comment, load up the comment template.
        if ( comments_open() || get_comments_number() ) {
            comments_template();
        }

        // End of the loop.
    endwhile;
    ?>

</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer();
// Adding my own session below
$_SESSION['menu_lang'] = "english";

I'm trying to print that session on mini-cart.php like this

if (!isset($_SESSION['menu_lang'])) {
 echo "no session";
} else {
 echo $_SESSION['menu_lang'];
}

How can I pass my session to mini-cart?

You can do using following code.

Add below code in your theme's functions.php file

function register_session_new(){
    if( ! session_id() ) {
       session_start();
     }
 }

add_action('init', 'register_session_new');
$_SESSION['menu_lang'] = "english";

Use this line in your mini-cart.php file or wherever you want to use.

echo $_SESSION['menu_lang'] ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM