使用Wordpress Super Cache从缓存中排除动态值 [英] Exclude a dynamic value from cache with Wordpress Super Cache

查看:130
本文介绍了使用Wordpress Super Cache从缓存中排除动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Super Cache插件。



有一段时间我一直在寻找解决方案,但没有成功。我需要为文件 functions.php 中的一个函数禁用缓存。

  add_shortcode('custom_counter','example_shortcode'); 
function example_shortcode(){
//获取自定义计数器选项值
$ counter = get_option('wc-custom-counter');
返回'< span class =custom-counter>'。 $ counter。 'rub。< / span>';
}

这是在创建的自定义页面上使用的短代码。此短代码输出的数据必须不属于页面缓存。

解决方案

改编自,将通过ajax替换为计数器实际非缓存值。即使在缓存页面中,Javascript也始终保持活动状态,因此它可以通过Ajax或任何检测到的事件更改页面上所需的任何内容。因此,无需在插件设置中排除任何内容



替换代码:

  //短代码
add_shortcode('custom_counter','customer_counter_shortcode');
function customer_counter_shortcode(){
//开始缓冲
ob_start();

//使用woocommerce现有的动画微调器gif图标
$ loading_url = home_url('/ wp-content/plugins/woocommerce/assets/images/select2-spinner.gif');

//显示加载微调器图标+文本以替换为Ajax值
echo'< span class =custom-counter>
< img id =loading-imgsrc ='。$ loading_url。'alt =Loading ...style =opacity:0.5;显示:内联块; vertical-align:middle;/>
< span style =opacity:0.5;>'。_(loading ...)。'< / span>
< / span>';
?>
< script type =text / javascript>
jQuery(function($){
if(typeof woocommerce_params ===' undefined')
返回false;

$ .ajax({
类型:'POST',
url:woocommerce_params.ajax_url,
data:{
'action':'custom_counter',
'custom-counter':true,
},
成功:函数(结果){
$('。custom -counter')。text(result);
console.log('response:'+ result); //仅用于测试|要删除
},
错误:函数(错误){
console.log(错误); //仅用于测试|要删除
}
});
});
< /脚本>
<?php
返回ob_get_clean(); //返回缓冲的代码
}

// wordpress ajax挂钩函数(对于已登录和未登录的用户)
add_action('wp_ajax_custom_counter','ajax_custom_counter');
add_action('wp_ajax_nopriv_custom_counter','ajax_custom_counter');
函数ajax_custom_counter(){
if(isset($ _ POST ['custom-counter'])&& $ _POST ['custom-counter'])
echo get_option('wc -custom-counter'); //获取选项值

exit();
}

代码进入活动子主题(或活动主题)的function.php文件)。测试和工作。


I'm using the Super Cache plugin.

For some time I was looking for a solution, but without success. I need to disable the cache for one function in the file functions.php.

add_shortcode('custom_counter', 'example_shortcode');
function example_shortcode() {
    // Get custom counter option value
    $counter = get_option( 'wc-custom-counter' );
    return '<span class="custom-counter">' . $counter . ' rub.</span>';
}

This is shortcode is used on the created custom page. It is necessary that the data output by this shortcode does not fall into the page cache.

解决方案

Adapted from this old WSE thread, you will find bellow the complete way to make it working.

Here we display a spinner loading icon that will be replaced by the counter real non cached value through ajax. Javascript stays always active even in a cached page, so it can change anything needed on the page via Ajax or via any detected event. So there is no need to exclude anything in the plugin settings.

The replacement code:

// The shortcode
add_shortcode('custom_counter', 'customer_counter_shortcode');
function customer_counter_shortcode() {
    // Start buffering
    ob_start(); 

    // Using woocommerce existing animated spinner gif icon
    $loading_url = home_url( '/wp-content/plugins/woocommerce/assets/images/select2-spinner.gif' );

    // Displaying a "Loading spinner icon + text to be replaced by Ajax value
    echo '<span class="custom-counter">
        <img id="loading-img" src="'.$loading_url.'" alt="Loading..." style="opacity:0.5; display:inline-block; vertical-align: middle;" />
        <span style="opacity:0.5;"> ' . _("loading…") . '</span>
    </span>';
    ?>
    <script type="text/javascript">
    jQuery( function($){
        if (typeof woocommerce_params === 'undefined')
            return false;

        $.ajax({
            type: 'POST',
            url: woocommerce_params.ajax_url,
            data: {
                'action': 'custom_counter',
                'custom-counter': true,
            },
            success: function (result) {
                $('.custom-counter').text(result);
                console.log('response: '+result); // just for testing | TO BE REMOVED
            },
            error: function(error){
                console.log(error); // just for testing | TO BE REMOVED
            }
        });
    });
    </script>
    <?php
    return ob_get_clean(); // Return the buffered code
}

// The wordpress ajax hooked function (for logged in and non logged users)
add_action('wp_ajax_custom_counter', 'ajax_custom_counter');
add_action('wp_ajax_nopriv_custom_counter', 'ajax_custom_counter');
function ajax_custom_counter() {
    if( isset($_POST['custom-counter']) && $_POST['custom-counter'] )
        echo get_option( 'wc-custom-counter' ); // Get option value

    exit();
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

这篇关于使用Wordpress Super Cache从缓存中排除动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆