O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

WordPress Transients

572 visualizações

Publicada em

A quick and easy guide to WordPress Transients

Publicada em: Software
  • Entre para ver os comentários

WordPress Transients

  1. 1. Transients: Temporary Cache Assistance
  2. 2. “... offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted.”
  3. 3. set_transient( $transient, $value, $expiration );
  4. 4. $transient • (string) a unique identifier for your cached data • 45 characters or less
  5. 5. $value • (array|object) Data to save, either a regular variable or an array/object • Handles serialization of complex data for you
  6. 6. $expiration • (integer) number of seconds to keep the data before refreshing • Example: 60*60*24 or 86400 (24 hours)
  7. 7. MINUTE_IN_SECONDS = 60 HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDS DAY_IN_SECONDS = 24 * HOUR_IN_SECONDS WEEK_IN_SECONDS = 7 * DAY_IN_SECONDS YEAR_IN_SECONDS = 365 * DAY_IN_SECONDS
  8. 8. get_transient($transient); • Returns false if the transient doesn’t exist or has expired • An integar value of zero/empty could be the stored data, so always check for false • Should not be used to hold plain boolean values; use array, string, or integer instead
  9. 9. delete_transient($transient);
  10. 10. Once a transient expires, it remains in the database until it is refreshed or deleted.
  11. 11. Basic Example: Store a query
  12. 12. function ut_get_ticker_items() { $args = array( 'post_type' => array( 'news-items', 'event' ), 'posts_per_page' => 99, 'taxonomy' => 'news_categories', 'term' => 'ticker', ); $posts = new WP_Query( $args ); return $posts; }
  13. 13. function ut_get_ticker_items() { if ( false === ( $posts = get_transient( 'ticker_items' ) ) ) { $args = array( 'post_type' => array( 'news-items', 'event' ), 'posts_per_page' => 99, 'taxonomy' => 'news_categories', 'term' => 'ticker', ); $posts = new WP_Query( $args ); set_transient( 'ticker_items', $posts, WEEK_IN_SECONDS ); } return $posts; }
  14. 14. function ut_refresh_ticker_items( $post_id, $post_obj ) { $post_type = $post_obj->post_type; if ( in_array( $post_type, array( 'news-items', 'event' ) ) ) { delete_transient( 'ticker_items' ); } } add_action( 'save_post', 'ut_refresh_ticker_items', 10, 2 ); add_action( 'deleted_post', 'ut_refresh_ticker_items', 10, 2 );
  15. 15. Advanced Example: Store user’s search results
  16. 16. function ws_set_search_string() { global $wp_query; $wp_session = WP_Session::get_instance(); if ( is_admin() === false && is_search() === true && isset( $wp_query->query_vars_hash ) === true ) { $search_string = $wp_query->query['s']; $search_hash = $wp_query->query_vars_hash; $wp_session['search_string'] = $search_string; $wp_session['search_hash'] = $search_hash; $wp_session['search_array'] = ws_search_array( $search_hash ); } if ( is_admin() === false && is_search() === false && is_attachment() === false && is_ main_query() === true ) { $wp_session['search_string'] = null; $wp_session['search_hash'] = null; $wp_session['search_array'] = null; } } add_action( 'pre_get_posts', 'ws_set_search_string' );
  17. 17. function ws_search_array( $search_hash ) { if ( false === ( $search_array = get_transient( 'ws_search_' . $search_hash ) ) ) { global $wpdb; $posts = $wpdb->get_results( $GLOBALS['wp_query']->request ); if ( empty( $posts ) === false ) { $search_array = array(); foreach ( $posts as $post ) { $search_array[] = $post->ID; } } // save the transient for 10 minutes set_transient( 'ws_search_' . $search_hash, $search_array, HOURS_IN_SECONDS * 6 ); } return $search_array; }
  18. 18. function ws_get_next_search_result( $next = true ) { $wp_session = WP_Session::get_instance(); if ( isset( $wp_session['search_array'] ) === false ) { return false; } $next_location = 0; $search_array = $wp_session['search_array']->toArray(); $location = array_search( get_the_ID(), $search_array ); if ( $next === true ) { $next_location = $location + 1; if ( isset( $search_array[$next_location] ) === false ) { $next_location = 0; } } else { $next_location = $location - 1; if ( isset( $search_array[$next_location] ) === false ) { end( $search_array ); $next_location = key( $search_array ); } } return $search_array[$next_location]; }
  19. 19. function ws_post_nav() { $prev = $next = null; if ( ws_get_next_search_result() ) { $prev = get_post( ws_get_next_search_result( false ) ); $next = get_post( ws_get_next_search_result() ); } else { $prev = get_previous_post( true ); $next = get_next_post( true ); } // output previous and next post links with get_permalink( $next->ID ) }
  20. 20. The End

×