How do I declare a global variable in PHP I can use across templates?

前端 未结 5 1746
逝去的感伤
逝去的感伤 2020-12-29 02:47

In wordpress, it calls header.php then whatever template name or the page requested. How can I declare a variable in my php header which I can then refer to in my other tem

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 03:19

    I think php session is one of the way to use as a global variable on Wordpress.

    1. Functions.php

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

    2. Any .php file where you want set or get global variables

    • get variable

      if(isset($_SESSION['global_var'])) {
          $local_var = $_SESSION['global_var'];
      }
      
    • set variable

      $_SESSION['global_var'] = "This is global var";
      

提交回复
热议问题