Ask your WordPress questions! Pay money and get answers fast! (more info)

Reliable transients WordPress

I have a public plugin that needs to check for update. Function below - get_version_to_update is fired when someone enters plugin admin page. In theory after request is done, data should be cached via transients for 24h. And this works fine for most sites, but for some specific sites I get data that wp_remote_request fires over and over, which (as I think) means that transient isn't saving.

What is more reliable way to check/save this?

public function get_version_to_update(){

// I get the transient
$data = get_transient("my_plugin_remote_version");

// I check if it exists
if($data === false){
// here I get data from server using wp_remote_request, server return just version number, e.g. "2.0"
$data = wp_remote_request(...);

// I cache it for 24h
set_transient("my_plugin_remote_version", $data, 60 * 60 * 24);
}

// I check if returned data from server is correct
if ( !$data || is_wp_error( $data ) || 200 != $data['response']['code']) {
return false;
} else {
if( version_compare(MY_PLUGIN_VERSION, $new_version, '<') ) {
// return new version, if it's newer then current
return $data['body'];
} else {
return false;
}
}
}


<strong>The main problem is that I can't test this. I don't have an access to sites that send a bunch of requests.</strong> I only know that it's not related to specific version of WordPress/PHP/Mysql.

Thank you!

Answers (3)

2012-12-22

phppoet answers:

what happen if you remove space between expiration time ?

set_transient("my_plugin_remote_version", $data, 60*60*24);


set_site_transient works instead of set_transient network wide when using WP Multisite .


phppoet comments:

Check this resource . it might help you

http://www.gilluminate.com/2011/12/23/host-your-own-custom-wordpress-plugin-updater/

2012-12-22

Martin Pham answers:

Hi there,
Please try this

public function get_version_to_update() {
$data = get_transient("my_plugin_remote_version");
if ( ! $data ) {
$response = wp_remote_post( ... );
$data = wp_remote_retrieve_body( $response );

/** error, store for 1h and return*/
if ( 'error' == $data || is_wp_error( $data ) || ! is_serialized( $data ) ) {
set_transient( 'my_plugin_remote_version', array( 'new_version' => MY_PLUGIN_VERSION ), 60 * 60 );
return false;
}
/** else, unserialize */
$data = maybe_unserialize( $data );

/** And store in transient for 24 hours */
set_transient( 'my_plugin_remote_version', $data , 60 * 60 * 24 );
}

/** Return false, if plugin up to date */
if( version_compare( MY_PLUGIN_VERSION , $data['new_version'] , '>=') ) {
return false;
}

return $data;
}


Martin Pham comments:

example code for remote request
/** remove_plugin_checking.php */

# do some thing

$_plugin = array(
'new_version' => '1.0.1',
'changelog_uri' => 'http://domain.com/changelog.php?v=1.0.1'
);
echo serialize($_plugin);


2012-12-22

Dylan Kuhn answers:

I've found that some caching plugins break the transient API. I use [[LINK href="https://gist.github.com/4360585"]]this custom page template[[/LINK]] to test whether the API is working on a particular server.