对WordPress主题选项使用缓存

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
function my_get_cache_option($option_name = 'ThemeAdminOptions' ){
    // get wanted transient
    $value = get_transient( $option_name  );
    // check if it has any content
    if(false === $value){
        // if no content in the transient get new copy of wanted option
        $value = get_option( $option_name );
        // set new transient with a refresh of 1 day
        set_transient( $option_name, $value, 60*60*24 );
    }
    // return the transient $value or newly created $value
    return $value;
}
?>
1
2
3
4
5
6
7
<?php
// get cached option
$options =  my_get_cache_option('WantedOptionName');
// do code with options array/object/string
// example code
echo $options['google_analytics_id'];
?>
1
2
3
4
5
6
7
8
<?php
// update the option as normal
update_option('WantedOptionName', $value );

// delete the transient that may be set
delete_transient('WantedOptionName');

?>

  •   正在提交中,请稍候...
      评论提交成功
    回复 的评论,点击取消回复

    账号密码登录