返回介绍

set_transient()

发布于 2017-09-11 10:22:35 字数 7758 浏览 1130 评论 0 收藏 0

set_transient( string $transient,  mixed $value,  int $expiration )

Set/update the value of a transient.


description

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is set.


参数

$transient

(string) (Required) Transient name. Expected to not be SQL-escaped. Must be 172 characters or fewer in length.

$value

(mixed) (Required) Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped.

$expiration

(int) (Optional) Time until expiration in seconds. Default 0 (no expiration).


返回值

(bool) False if value was not set and true if value was set.


源代码

File: wp-includes/option.php

function set_transient( $transient, $value, $expiration = 0 ) {

	$expiration = (int) $expiration;

	/**
	 * Filters a specific transient before its value is set.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 3.0.0
	 * @since 4.2.0 The `$expiration` parameter was added.
	 * @since 4.4.0 The `$transient` parameter was added.
	 *
	 * @param mixed  $value      New value of transient.
	 * @param int    $expiration Time until expiration in seconds.
	 * @param string $transient  Transient name.
	 */
	$value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration, $transient );

	/**
	 * Filters the expiration for a transient before its value is set.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 4.4.0
	 *
	 * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.
	 * @param mixed  $value      New value of transient.
	 * @param string $transient  Transient name.
	 */
	$expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient );

	if ( wp_using_ext_object_cache() ) {
		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
	} else {
		$transient_timeout = '_transient_timeout_' . $transient;
		$transient_option = '_transient_' . $transient;
		if ( false === get_option( $transient_option ) ) {
			$autoload = 'yes';
			if ( $expiration ) {
				$autoload = 'no';
				add_option( $transient_timeout, time() + $expiration, '', 'no' );
			}
			$result = add_option( $transient_option, $value, '', $autoload );
		} else {
			// If expiration is requested, but the transient has no timeout option,
			// delete, then re-create transient rather than update.
			$update = true;
			if ( $expiration ) {
				if ( false === get_option( $transient_timeout ) ) {
					delete_option( $transient_option );
					add_option( $transient_timeout, time() + $expiration, '', 'no' );
					$result = add_option( $transient_option, $value, '', 'no' );
					$update = false;
				} else {
					update_option( $transient_timeout, time() + $expiration );
				}
			}
			if ( $update ) {
				$result = update_option( $transient_option, $value );
			}
		}
	}

	if ( $result ) {

		/**
		 * Fires after the value for a specific transient has been set.
		 *
		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
		 *
		 * @since 3.0.0
		 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
		 * @since 4.4.0 The `$transient` parameter was added.
		 *
		 * @param mixed  $value      Transient value.
		 * @param int    $expiration Time until expiration in seconds.
		 * @param string $transient  The name of the transient.
		 */
		do_action( "set_transient_{$transient}", $value, $expiration, $transient );

		/**
		 * Fires after the value for a transient has been set.
		 *
		 * @since 3.0.0
		 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
		 *
		 * @param string $transient  The name of the transient.
		 * @param mixed  $value      Transient value.
		 * @param int    $expiration Time until expiration in seconds.
		 */
		do_action( 'setted_transient', $transient, $value, $expiration );
	}
	return $result;
}

更新日志

Versiondescription
2.8.0Introduced.

相关函数

Uses

  • wp-includes/option.php: expiration_of_transient_{$transient}
  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/load.php: wp_using_ext_object_cache()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: do_action()
  • wp-includes/option.php: pre_set_transient_{$transient}
  • wp-includes/option.php: set_transient_{$transient}
  • wp-includes/option.php: setted_transient
  • wp-includes/option.php: add_option()
  • wp-includes/option.php: delete_option()
  • wp-includes/option.php: update_option()
  • wp-includes/option.php: get_option()
  • Show 7 more uses Hide more uses

Used By

  • wp-includes/class-wp-oembed-controller.php: WP_oEmbed_Controller::get_proxy_item()
  • wp-admin/includes/class-wp-plugins-list-table.php: WP_Plugins_List_Table::prepare_items()
  • wp-admin/includes/theme-install.php: install_themes_feature_list()
  • wp-admin/includes/dashboard.php: wp_dashboard_cached_rss_widget()
  • wp-admin/includes/deprecated.php: wp_dashboard_plugins_output()
  • wp-includes/cron.php: spawn_cron()
  • wp-includes/pluggable.php: wp_rand()
  • wp-includes/class-wp-feed-cache-transient.php: WP_Feed_Cache_Transient::save()
  • wp-includes/class-wp-feed-cache-transient.php: WP_Feed_Cache_Transient::touch()
  • wp-includes/media.php: wp_maybe_generate_attachment_metadata()
  • wp-includes/ms-functions.php: get_dirsize()
  • wp-includes/author-template.php: is_multi_author()
  • wp-includes/rss.php: RSSCache::set()
  • Show 8 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Nicola Mustone

    This example shows how to set a transient with the latest five blog posts. It expires after one day.
    It uses time constants to set the expiration time.

    
    // Set the arguments for the custom query
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => 5,
        'orderby'        => 'date',
        'order'          => 'DESC'
    );
    $latest_post = new WP_Query( $args );
    
    // Save the results in a transient named latest_5_posts
    set_transient( 'latest_5_posts', $latest_post, DAY_IN_SECONDS );
    

    To know more about how to get posts and custom post type items read the documentation of WP_Query.

  2. Saving the $special_query_results object for 12 hours

    
    set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文