代码之家  ›  专栏  ›  技术社区  ›  Blues Clues

如何修复Wordpress中数据库中PHP数据的缓存问题

  •  0
  • Blues Clues  · 技术社区  · 4 年前

    我试图使用下面的代码进行插件开发,但存在缓存问题(我认为)。请在下面了解详情。

    问题

    我创建了一个过滤器来接受自定义的url,我正在查询数据库,但结果值似乎在浏览器中没有变化,而是在数据库中发生了变化。因此,基本上传递的数据不会从数据库返回更新的值。

    代码

    add_action( 'init',  function() {
        add_rewrite_rule('coupon/([a-zA-Z0-9]+)[/]?$', 'index.php?coupon=$matches[1]', 'top');
    });
    
    add_filter('query_vars', function( $query_vars ) {
        $query_vars[] = 'coupon';
        return $query_vars;
    });
    
    add_action( 'template_include', function( $template ) {
        if ( get_query_var( 'coupon' ) == false || get_query_var( 'coupon' ) == '' ) {
            return $template;
        }
    
        ob_clean();
        ob_start();
    
        // Check if coupon exists
        global $wpdb;
        $coupon = get_query_var('coupon');
    
        $coupon_query = $wpdb->prepare('SELECT * FROM wp_urls WHERE coupon = %s AND claimed_at IS NULL', $coupon);
        $coupon_available = $wpdb->query($coupon_query) ? true : false;
    
        if(!$coupon_available) {
            header('Location: /'); // Redirect to landing page
            exit();
        }
        
        return plugin_dir_path(__FILE__) . 'views/client/index.php';;
    } );
    

    预期结果

    预期结果应将用户重定向到 header('Location: /'); 如果优惠券不可用。

    知道吗?

    0 回复  |  直到 4 年前
        1
  •  0
  •   Dmitry Leiko    4 年前

    您需要更新WordPress管理区域中的永久链接。例如:

    Step 1: In the WordPress admin area, go to `Settings > Permalinks`
    Step 2: Click `Save Changes`
    Step 3: Permalinks and rewrite rules are flushed.