• I may be missing something really basic.

    My plugin will do database calls during filters that are earlier in the loop (‘modify_header_image_data’ & ‘wp_get_attachment_image_attributes’), and then repeat them during ‘wp_footer’.

    I’d rather do everything once, store it in a variable, let wp_footer read it.

    I’d expect there to be a per-page scope where users can create variables. But I don’t know what it is.

    • The plugin.php file seems to be loaded once, at startup, and assigns hooks & filters that run per-post (or more often).
    • The Post object doesn’t have a generic data field to put stuff on.
    • The Object Cache has an expiration measured in seconds, and seems to be for stuff that persists *between* pages.
    • The Transients API is even longer term, for pushing objects from memory into the database.

    What really obvious thing am I missing here?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The Object Cache can have a seconds-length expiry, but by default cached objects don’t expire. It’s exactly what you’re after, a cache that only persists for the life of the page. Note that it actually doesn’t persist between pages. It’s purely for storing data to be re-used later on the same page.

    Thread Starter Ian McDonald

    (@drianmcdonald)

    Thanks – that’s what I needed to know.

    Dion

    (@diondesigns)

    Using the object cache makes sense if the data will be used for multiple page loads. For a single page load, however, using the object cache would waste server resources. All that’s needed in that case is to save the data in a variable accessible to the footer function. Here’s some sample code which demonstrates how to do this.

    class myclass {
    	var $mydata;
    
    	function __construct() {
    		add_action('init', array($this, 'early_function'));
    		add_action('wp_footer', array($this, 'footer_function'));
    	}
    
    	function early_function() {
    		(crunch numbers, create variables $data1, $data2, and $data3)
    
    		$this->mydata = compact($data1, $data2, $data3);
    	}
    
    	function footer_function() {
    		extract($this->mydata);
    		
    		(do something with variables $data1, $data2, and $data3)
    	}
    }
    
    new myclass;

    Using the object cache makes sense if the data will be used for multiple page loads. For a single page load, however, using the object cache would waste server resources.

    I think you’re mistaking WordPress’ Object Cache API with ‘true’ object caching, like Memcache or Redis. What you’ve said might apply to those, but is not correct for the API.

    The WordPress Object Cache works basically the same way your code does. See the Codex article on it to see how it works.

    Dion

    (@diondesigns)

    Using an object cache may (but not always) produce the same result as using a global variable, but it will be at a much higher price. There is significant overhead in the WP cache system, which makes it less than desirable for non-persistent caching of data. Also, if an external cache has been defined, cached data will most likely be stored/persistent. (It’s why installing a cache plugin like W3TC has the potential to degrade a site’s performance.)

    Using the object cache for something like this seems a bit “Rube Goldbergian” to me. YMMV.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to persist data during the life of a page?’ is closed to new replies.