SlideShare une entreprise Scribd logo
1  sur  20
Télécharger pour lire hors ligne
Transients:
Temporary Cache Assistance
“... 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.”
set_transient(
$transient,
$value,
$expiration
);
$transient
•	 (string) a unique identifier for your cached data
•	 45 characters or less
$value
•	 (array|object) Data to save, either a regular variable or
an array/object
•	 Handles serialization of complex data for you
$expiration
•	 (integer) number of seconds to keep the data before
refreshing
•	 Example: 60*60*24 or 86400 (24 hours)
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
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
delete_transient($transient);
Once a transient expires, it remains in the
database until it is refreshed or deleted.
Basic Example:
Store a query
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;
}
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;
}
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 );
Advanced Example:
Store user’s search results
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' );
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;
}
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];
}
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 )
}
The End

Contenu connexe

Tendances

Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingErick Hitter
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An AnalysisJustin Finkelstein
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
Pm 4.0 permission_storage
Pm 4.0 permission_storagePm 4.0 permission_storage
Pm 4.0 permission_storageOleg K
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in LaravelHAO-WEN ZHANG
 
CodeIgniter Class Reference
CodeIgniter Class ReferenceCodeIgniter Class Reference
CodeIgniter Class ReferenceJamshid Hashimi
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Fabien Potencier
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design PatternsHugo Hamon
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2zfconfua
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQLddiers
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixturesBill Chang
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Editionddiers
 
Database API, your new friend
Database API, your new friendDatabase API, your new friend
Database API, your new friendkikoalonsob
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From IusethisMarcus Ramberg
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivityMouli Chandira
 
Speed Things Up with Transients
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with TransientsCliff Seal
 

Tendances (19)

Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
Sk.php
Sk.phpSk.php
Sk.php
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An Analysis
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Pm 4.0 permission_storage
Pm 4.0 permission_storagePm 4.0 permission_storage
Pm 4.0 permission_storage
 
Dependency Injection in Laravel
Dependency Injection in LaravelDependency Injection in Laravel
Dependency Injection in Laravel
 
CodeIgniter Class Reference
CodeIgniter Class ReferenceCodeIgniter Class Reference
CodeIgniter Class Reference
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Database Design Patterns
Database Design PatternsDatabase Design Patterns
Database Design Patterns
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
 
Drupal II: The SQL
Drupal II: The SQLDrupal II: The SQL
Drupal II: The SQL
 
Doctrine fixtures
Doctrine fixturesDoctrine fixtures
Doctrine fixtures
 
Drupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary EditionDrupal - dbtng 25th Anniversary Edition
Drupal - dbtng 25th Anniversary Edition
 
Database API, your new friend
Database API, your new friendDatabase API, your new friend
Database API, your new friend
 
Bag Of Tricks From Iusethis
Bag Of Tricks From IusethisBag Of Tricks From Iusethis
Bag Of Tricks From Iusethis
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
Speed Things Up with Transients
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with Transients
 

En vedette

Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentTammy Hart
 
Dont Forget the Milk
Dont Forget the MilkDont Forget the Milk
Dont Forget the MilkTammy Hart
 
Traversing Search Results
Traversing Search ResultsTraversing Search Results
Traversing Search ResultsTammy Hart
 
Responsify! 5 Things You Should Know About Responsive Web Design
Responsify! 5 Things You Should Know About Responsive Web DesignResponsify! 5 Things You Should Know About Responsive Web Design
Responsify! 5 Things You Should Know About Responsive Web DesignTammy Hart
 
Word Press And Multimedia
Word Press And MultimediaWord Press And Multimedia
Word Press And MultimediaTammy Hart
 
Freelancing with WordPress
Freelancing with WordPressFreelancing with WordPress
Freelancing with WordPressTammy Hart
 
Professioanl Blogging
Professioanl BloggingProfessioanl Blogging
Professioanl BloggingTammy Hart
 
Designing for WordPress
Designing for WordPressDesigning for WordPress
Designing for WordPressTammy Hart
 
Word Press & Working With Clients
Word Press & Working With ClientsWord Press & Working With Clients
Word Press & Working With ClientsTammy Hart
 
Food Blog Design
Food Blog DesignFood Blog Design
Food Blog DesignTammy Hart
 
Custom WordPress theme development
Custom WordPress theme developmentCustom WordPress theme development
Custom WordPress theme developmentTammy Hart
 
In Browser Design with WordPress
In Browser Design with WordPressIn Browser Design with WordPress
In Browser Design with WordPressTammy Hart
 

En vedette (13)

Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme development
 
Dont Forget the Milk
Dont Forget the MilkDont Forget the Milk
Dont Forget the Milk
 
Traversing Search Results
Traversing Search ResultsTraversing Search Results
Traversing Search Results
 
Responsify!
Responsify!Responsify!
Responsify!
 
Responsify! 5 Things You Should Know About Responsive Web Design
Responsify! 5 Things You Should Know About Responsive Web DesignResponsify! 5 Things You Should Know About Responsive Web Design
Responsify! 5 Things You Should Know About Responsive Web Design
 
Word Press And Multimedia
Word Press And MultimediaWord Press And Multimedia
Word Press And Multimedia
 
Freelancing with WordPress
Freelancing with WordPressFreelancing with WordPress
Freelancing with WordPress
 
Professioanl Blogging
Professioanl BloggingProfessioanl Blogging
Professioanl Blogging
 
Designing for WordPress
Designing for WordPressDesigning for WordPress
Designing for WordPress
 
Word Press & Working With Clients
Word Press & Working With ClientsWord Press & Working With Clients
Word Press & Working With Clients
 
Food Blog Design
Food Blog DesignFood Blog Design
Food Blog Design
 
Custom WordPress theme development
Custom WordPress theme developmentCustom WordPress theme development
Custom WordPress theme development
 
In Browser Design with WordPress
In Browser Design with WordPressIn Browser Design with WordPress
In Browser Design with WordPress
 

Similaire à WordPress Transients

[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...Mateusz Zalewski
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked aboutTatsuhiko Miyagawa
 
Why Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary ThingWhy Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary ThingChris Reynolds
 
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress WordCamp Kyiv
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmdiKlaus
 
Intro to advanced caching in WordPress
Intro to advanced caching in WordPressIntro to advanced caching in WordPress
Intro to advanced caching in WordPressMaor Chasen
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of TransductionDavid Stockton
 
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze..."Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...Mateusz Zalewski
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionIban Martinez
 
MUC - Moodle Universal Cache
MUC - Moodle Universal CacheMUC - Moodle Universal Cache
MUC - Moodle Universal CacheTim Hunt
 
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011camp_drupal_ua
 
Introduction to Zend Framework web services
Introduction to Zend Framework web servicesIntroduction to Zend Framework web services
Introduction to Zend Framework web servicesMichelangelo van Dam
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 DatasourceKaz Watanabe
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Masahiro Nagano
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of LithiumNate Abele
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesLuis Curo Salvatierra
 

Similaire à WordPress Transients (20)

[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
[PHPCon 2023] “Kto to pisał?!... a, to ja.”, czyli sposoby żeby znienawidzić ...
 
20 modules i haven't yet talked about
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked about
 
Why Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary ThingWhy Hacking WordPress Search Isn't Some Big Scary Thing
Why Hacking WordPress Search Isn't Some Big Scary Thing
 
Wp query
Wp queryWp query
Wp query
 
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
WordPress Kitchen 2014 - Александр Стриха: Кеширование в WordPress
 
Propel sfugmd
Propel sfugmdPropel sfugmd
Propel sfugmd
 
Intro to advanced caching in WordPress
Intro to advanced caching in WordPressIntro to advanced caching in WordPress
Intro to advanced caching in WordPress
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
 
Daily notes
Daily notesDaily notes
Daily notes
 
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze..."Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introduction
 
MUC - Moodle Universal Cache
MUC - Moodle Universal CacheMUC - Moodle Universal Cache
MUC - Moodle Universal Cache
 
Php
PhpPhp
Php
 
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
Nickolay Shmalenuk.Render api eng.DrupalCamp Kyiv 2011
 
Introduction to Zend Framework web services
Introduction to Zend Framework web servicesIntroduction to Zend Framework web services
Introduction to Zend Framework web services
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 
The State of Lithium
The State of LithiumThe State of Lithium
The State of Lithium
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móvilesDesarrollo de módulos en Drupal e integración con dispositivos móviles
Desarrollo de módulos en Drupal e integración con dispositivos móviles
 

Dernier

Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 

Dernier (20)

Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 

WordPress Transients

  • 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.”
  • 4. $transient • (string) a unique identifier for your cached data • 45 characters or less
  • 5. $value • (array|object) Data to save, either a regular variable or an array/object • Handles serialization of complex data for you
  • 6. $expiration • (integer) number of seconds to keep the data before refreshing • Example: 60*60*24 or 86400 (24 hours)
  • 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. 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
  • 10. Once a transient expires, it remains in the database until it is refreshed or deleted.
  • 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. 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. 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 );
  • 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. 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. 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. 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 ) }