get_site_transient()WP 2.9.0

Gets the value of the temporary option of the main site in the network.

If object caching is used on the site, the temporary options will be retrieved from the cache; otherwise, the data is obtained using get_site_option() (see the logic in the description).

Use the function get_transient() when you need to get the temporary option of the current site, not the main site in the network.

The difference between get_transient() and get_site_transient() is the same as between get_option() and get_network_option().

Returns

Mixed.

  • false is returned in the following cases:

    • if the temporary option does not exist
    • or it has an empty value
    • or it has expired.
  • In other cases, it returns the obtained option value.

Note: the returned false should be checked with strict equality (===), not regular double equality, because the option value may contain 0 or an empty string, an empty array. For this reason, do not save false in the option value; instead, save 0 or place false in an array.

Usage

get_site_transient( $transient );
$transient(string) (required)
The name of the temporary option.

Examples

0

#1 Example of use

Suppose we have data that is used on all sites on the network. This data needs to be retrieved by HTTP request from another site, so it is good to cache this request. This function will be good for this purpose. The logic of such code is shown below:

$transient = 'some_trans_name';
$remote_data = get_site_transient( $transient );

if( false === $remote_data ){
	$remote_data = file_get_contents( 'http://dom.ru/promo/yith-promo.xml' );
}

if( $remote_data ){
	set_site_transient( $transient, $remote_data, WEEK_IN_SECONDS );
}

Notes

Changelog

Since 2.9.0 Introduced.

get_site_transient() code WP 7.0

function get_site_transient( $transient ) {

	/**
	 * Filters the value of an existing site transient before it is retrieved.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * Returning a value other than boolean false will short-circuit retrieval and
	 * return that value instead.
	 *
	 * @since 2.9.0
	 * @since 4.4.0 The `$transient` parameter was added.
	 *
	 * @param mixed  $pre_site_transient The default value to return if the site transient does not exist.
	 *                                   Any value other than false will short-circuit the retrieval
	 *                                   of the transient, and return that value.
	 * @param string $transient          Transient name.
	 */
	$pre = apply_filters( "pre_site_transient_{$transient}", false, $transient );

	if ( false !== $pre ) {
		return $pre;
	}

	if ( wp_using_ext_object_cache() || wp_installing() ) {
		$value = wp_cache_get( $transient, 'site-transient' );
	} else {
		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.
		$no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );
		$transient_option = '_site_transient_' . $transient;
		if ( ! in_array( $transient, $no_timeout, true ) ) {
			$transient_timeout = '_site_transient_timeout_' . $transient;
			wp_prime_site_option_caches( array( $transient_option, $transient_timeout ) );

			$timeout = get_site_option( $transient_timeout );
			if ( false !== $timeout && $timeout < time() ) {
				delete_site_option( $transient_option );
				delete_site_option( $transient_timeout );
				$value = false;
			}
		}

		if ( ! isset( $value ) ) {
			$value = get_site_option( $transient_option );
		}
	}

	/**
	 * Filters the value of an existing site transient.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 2.9.0
	 * @since 4.4.0 The `$transient` parameter was added.
	 *
	 * @param mixed  $value     Value of site transient.
	 * @param string $transient Transient name.
	 */
	return apply_filters( "site_transient_{$transient}", $value, $transient );
}