dtfo55908 2016-05-17 08:40
浏览 13
已采纳

基于访问过的URL个性化Wordpress会话

first time Question asker here. I'm building a web page and want to change the text on page if the user has visited certain pages.

My initial idea was too create an array in the session which records each url as its visited with something like $_SERVER['REQUEST_URI'], which would probably work if it was a site I was building from scratch. however... Because the site is built in Wordpress I'm not 100% how to go about doing that within there system.

  • 写回答

1条回答 默认 最新

  • doutu9810 2016-05-17 10:48
    关注

    Now here's what I'd suggest you do. Navigate to your Theme and open the file functions.php Then find a suitable spot anywhere on the File (The bottom of the file wouldn't be that odd). Then, add the following functions:

        <?php
    
            // FILE-NAME: functions.php  <== LOCATED AT THE ___/wp-content/themes/your-theme-name 
            add_action("init",   "initiatePageLogging");
    
            function initiatePageLogging(){
                // START THE SESSION IF IT HAS NOT BEEN STARTED
                // THIS WOULD BE USED TO SHARE DATA ACROSS YOUR PAGES...
                if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
                    session_start();
                }
    
                // CHECK THAT THE SESSION VARIABLE FOR OUR PAGE-LOGGING IS THERE
                // IF NOT CREATE IT
                if(!isset($_SESSION['visitedPages'])){
                    // IT DOES NOT EXIST SO WE CREATE IT & INITIALIZE IT AS AN EMPTY ARRAY
                    $_SESSION['visitedPages'] = array();
                }
                // NO NEED TO KEEP THE SESSION ARRAY $_SESSION['visitedPages'] TOO LONG
                // SO WE TRIM IT OUT CONDITIONALLY TO KEEP IT UNDER CHECK
                if(count($_SESSION['visitedPages']) >= 10){
                    // WE REMOVE ABOUT 7 ELEMENTS FROM THE BEGINNING OF THE ARRAY
                    // LEAVING JUST THE LAST 3 - NOW THAT'S COOL...
                    $arrVisitedPages            = $_SESSION['visitedPages'];
                    array_splice($arrVisitedPages, 0, 7);
                    // RE-DEFINE THE $_SESSION['visitedPages'] ARRAY
                    $_SESSION['visitedPages']   = $arrVisitedPages;
                }
            }
    
            function getLastVisitedPage(){
                $lastVisitedPage        = get_site_url() . $_SERVER['REQUEST_URI'];       //<== FALL BACK TO THE CURRENT PAGE IF WE HAVE AN ISSUE.
                if( isset($_SESSION['visitedPages']) && is_array($_SESSION['visitedPages']) ){
                    $arrVP              = $_SESSION['visitedPages'];
                    $intArrVPLength     = count($arrVP);
                    $diff               = ($intArrVPLength - 2);
                    $lastVisitedPage    = ( $intArrVPLength > 1) ? $arrVP[$diff]  : $lastVisitedPage;
                }
                return $lastVisitedPage;
            }
    
        ?>
    

    Now, Part 1 is done! Inside of your Theme, You will still find a File Called header.php This is where we have to do the logging because by default every page in Word-Press loads this Page (except when configured otherwise). At the very, very top of that File - I mean on Line 1, do this:

        <?php
            // FILE-NAME: header.php  <== LOCATED AT THE ___/wp-content/themes/your-theme-name 
    
            // BUILD THE URL OF THE CURRENT PAGE & PUSH IT TO THE SESSION VARIABLE...
            $thisPage                   = get_site_url() . $_SERVER['REQUEST_URI'];
            $_SESSION['visitedPages'][] = $thisPage;
    
           // THAT'S ALL! BELOW HERE, THE ORIGINAL CONTENT OF THE header.php FILE CONTINUES...
    
        ?>
    

    One more thing! How do we now use the $_SESSION['visitedPages'] Variable? Otherwise, how do we know which page was last visited using the $_SESSION['visitedPages'] Variable?

    Now on every File like (page.php, index.php, category.php, taxonomy.php, etc); you can now find out the last visited Page by doing something like this:

        <?php
    
            // FILE-NAME: ANY FILE IN THE THEME LIKE:  page.php, index.php, category.php, taxonomy.php, etc
            $lastVisitedPage        = getLastVisitedPage();
            // THAT'S IT, PAL...
        ?>
    

    I hope this helps....

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀