Variable from a plugin into a theme

First plugin, basic question… I am trying to print the variable $xavi from a plugin into a theme like this:

add_action('init', '_load_options');

function _load_options() {
  global $xavi;
}

So that I can use it in a theme file like this:

Read More
<?=$xavi;?>

It doesn’t look like init is the right action hook for this but the only other one that makes sense is wp_head and doesn’t work either

Any ideas which hook should I use or how should I define variables (maybe through an existing WP function/method instead of creating one from scratch)?

Thanks!

Related posts

Leave a Reply

4 comments

  1. Your webhost has disabled register_globals in the php.ini, so you have to “register” your variables manually everytime you want to use it. For example:

    <?php
    global $xavi;
    echo $xavi;
    ?>
    

    Another approach is using the $GLOBALS array:

    <?php
    echo $GLOBALS['xavi'];
    ?>
    

    But in my opinion, you should avoid using globals. Instead use a simple registry-class, which you can add/get your values.

    EDIT

    This isn’t a WordPress specific solution and could be a bit of an overkill for this simple question. The registry pattern is an official programming design-pattern. Same goes for the singleton-pattern which I’ve used here too.

    What we do here is to store our content in a registry-object. It’s singleton, so only one instance is created.

    I know, the problem isn’t solved really. Instead of using the $GLOBALS array, we are using a registry class which is indeed also “global” as we call the instance everytime we need it. You don’t have control, where you call it. Also the testing of a singleton class could be problematic. If you want more control, just look at the factory-pattern with dependancy injection.

    class Webeo_Registry {
        /**
         * Array to store items
         * 
         * @var array
         */
        private $_objects = array();
    
        /**
         * Stores the registry object
         * 
         * @var Webeo_Registry
         */
        private static $_instance = null;
    
        /**
         * Get the instance
         * 
         * @return void
         */
        public static function getInstance() {
            if (is_null(self::$_instance)) {
                self::$_instance = new self();
            }
    
            return self::$_instance;
        }
    
        /**
         * Constructor
         * 
         * @access private
         * @return void
         */
        private function __construct() {}
    
        /**
         * Set an item by given key and value
         * 
         * @param string $key
         * @param void $value
         * @return void
         */
        public function __set($key, $value) {
            // maybe you want to check if the value already exists
            $this->_objects[$key] = $value;
        }
    
        /**
         * Get an item by key
         * 
         * @param string $key Identifier for the registry item
         * @return void
         */
        public function __get($key) {
            if(isset($this->_objects[$key])) {
                $result = $this->_objects[$key];
            } else {
                $result = null;
            }
    
            return $result;
        }
    
        /**
         * Check if an item with the given key exists
         * 
         * @param string $key
         * @return void
         */
        public function __isset($key) {
            return isset($this->_objects[$key]);
        }
    
        /**
         * Delete an item with the given key
         * 
         * @param string $key
         * @return void
         */
        public function __unset($key) {
            unset($this->_objects[$key]);
        }
    
        /**
         * Make clone private, so nobody can clone the instance
         * 
         * @return void
         */
        private function __clone() {}
    }
    

    In your plugin/theme, you only have to return the instance and you’re ready to use it:

    $registry = Webeo_Registry::getInstance();
    
    // Define some options (just add it after the "->". The magic method __set will do the rest)
    $registry->optionA = 'awdawd';
    $registry->optionB = array(1,2,3);
    
    // Check the content
    print_r($registry);
    
    // Remove an item
    unset($registry->optionA);
    
    // Check if option exists
    if(isset($registry->optionA)) {
        echo 'option exist';
    } else {
        echo 'option does not exist';
    }
    
  2. Another option would be to use a shortcode. The Wodrpess Codex talks through all the specifics. But basically setup a function that returns your variable. Add the shortcode to wordpress, then call do_shortcode( '[shortcode]' ) inside your PHP code and you can echo it or store it to process it more. You can use use the shortcode inside content.

  3. You can use functions in themes. If I’m correct you can add the following in your template:

    <?php _load_options() ?> 
    

    Should work. However the _ probably means that it’s private, so you might need to make the function public.