setting variable in header.php but not seen in footer.php

后端 未结 5 1207
醉梦人生
醉梦人生 2021-02-19 02:24

in wordpress , i set a variable in header.php


but in footer.php when I ech

相关标签:
5条回答
  • 2021-02-19 02:42

    You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).

    So just declare your variable as global:

    $GLOBALS[ 'var' ] = '...';
    

    Then:

    echo $GLOBALS[ 'var' ];
    
    0 讨论(0)
  • 2021-02-19 02:47

    try this code

    first define your initial variable

      $var="something";
    

    then use the $_GLOBALS

      $_GLOBALS['myvar']=$var;
    

    and finally use the global variable in anywhere you want

      global $myvar;
    

    define string inside the $_GLOBALS as taken as global variable name or use the $_GLOBALS['myvar'] direct into the code without using the global

    0 讨论(0)
  • 2021-02-19 02:55

    I know this is a bit old question and with a solution voted but I though I should share another option and just found a better solution (that works) without using Globals

    function fn_your_var_storage( $var = NULL )
    {
        static $internal;
    
        if ( NULL !== $var )
        {
            $internal = $var;
        }
    
        return $internal;
    }
    
    // store the value
    fn_your_var_storage( 'my_value' );
    
    // retrieve value
    echo fn_your_var_storage(); // print my_value
    
    0 讨论(0)
  • 2021-02-19 03:01

    I know you've already accepted the answer to this question; however, I think there's a much better approach to the variable scope problem than passing vars into the $GLOBALS array.

    Take the functions.php file in your theme for example. This file is included outside the scope of the get_header() and get_footer() functions. In fact it supersedes anything else you might be doing in your theme (and I believe in the plugin scope as well--though I'd have to check that.)

    If you want to set a variable that you'd like to use in your header/footer files, you should do it in your functions.php file rather than polluting $GLOBALS array. If you have more variables that you want to sure, consider using a basic Registry object with getters/setters. This way your variables will be better encapsulated in a scope you can control.

    Registry

    Here's a sample Registry class to get you started if:

    <?php
    /**
     * Registry
     *
     * @author Made By Me
     * @version v0.0.1
     */
    class Registry
    {
        # +------------------------------------------------------------------------+
        # MEMBERS
        # +------------------------------------------------------------------------+
        private $properties = array();
    
        # +------------------------------------------------------------------------+
        # ACCESSORS
        # +------------------------------------------------------------------------+
        /**
         * @set     mixed   Objects
         * @param   string  $index  A unique index
         * @param   mixed   $value  Objects to be stored in the registry
         * @return  void
         */
        public function __set($index, $value)
        {
            $this->properties[ $index ] = $value;
        }
    
        /**
         * @get     mixed   Objects stored in the registry
         * @param   string  $index  A unique ID for the object
         * @return  object  Returns a object used by the core application.
         */
        public function __get($index)
        {
            return $this->properties[ $index ];
        }
    
        # +------------------------------------------------------------------------+
        # CONSTRUCTOR
        # +------------------------------------------------------------------------+
        public function __construct()
        {
        }
    
    
    }
    

    Save this class in your theme somewhere, e.g. /classes/registry.class.php Include the file at the top of your functions.php file: include( get_template_directory() . '/classes/registry.class.php');

    Example Usage

    Storing variables:

    $registry = new Registry();
    $registry->my_variable_name = "hello world";
    

    Retrieving variables:

    echo '<h1>' .  $registry->my_variable_name . '</h1>'
    

    The registry will accept any variable type.

    Note: I normally use SplObjectStorage as the internal datastore, but I've swapped it out for a regular ole array for this case.

    0 讨论(0)
  • 2021-02-19 03:01

    In wordpress Header, any template, Footer is different functions so you have to declare any varible as a global variable then you can access it .

    /** header.php **/
    <?php
    global $xyz;
    $xyz="123456"; ?>
    
    /** Template.php or Footer.php **/
    <?php 
    echo $xyz; ///return 123456
    ?>
    
    0 讨论(0)
提交回复
热议问题