├── README.md └── gwp_widget_cache.php /README.md: -------------------------------------------------------------------------------- 1 | GWP-Widget-Cache 2 | ================ 3 | 4 | A plugin to cache WordPress Widgets using the Transients API, based on this tutorial http://generatewp.com/?p=10132 5 | -------------------------------------------------------------------------------- /gwp_widget_cache.php: -------------------------------------------------------------------------------- 1 | is_preview() ){ 58 | return $instance; 59 | } 60 | //check if we need to cache this widget? 61 | if(isset($instance['wc_cache']) && $instance['wc_cache'] == true) 62 | return $instance; 63 | 64 | //simple timer to clock the widget rendring 65 | $timer_start = microtime(true); 66 | 67 | //create a uniqe transient ID for this widget instance 68 | $transient_name = $this->get_widget_key($instance,$args); 69 | 70 | //get the "cached version of the widget" 71 | if ( false === ( $cached_widget = get_transient( $transient_name ) ) ){ 72 | // It wasn't there, so render the widget and save it as a transient 73 | // start a buffer to capture the widget output 74 | ob_start(); 75 | //this renders the widget 76 | $widget->widget( $args, $instance ); 77 | //get rendered widget from buffer 78 | $cached_widget = ob_get_clean(); 79 | //save/cache the widget output as a transient 80 | set_transient( $transient_name, $cached_widget, $this->cache_time); 81 | } 82 | 83 | //output the widget 84 | echo $cached_widget; 85 | //output rendering time as an html comment 86 | echo ''; 87 | 88 | //after the widget was rendered and printed we return false to short-circuit the normal display of the widget 89 | return false; 90 | } 91 | 92 | /** 93 | * in_widget_form 94 | * this method displays a checkbox in the widget panel 95 | * 96 | * @param WP_Widget $t The widget instance, passed by reference. 97 | * @param null $return Return null if new fields are added. 98 | * @param array $instance An array of the widget's settings. 99 | * 100 | */ 101 | function in_widget_form($t,$return,$instance){ 102 | $instance = wp_parse_args( 103 | (array) $instance, 104 | array( 105 | 'title' => '', 106 | 'text' => '', 107 | 'wc_cache' => null 108 | ) 109 | ); 110 | 111 | if ( !isset($instance['wc_cache']) ) 112 | $instance['wc_cache'] = null; 113 | ?> 114 |
115 | /> 116 | 117 |
118 |