最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Preventing direct access to a page in WordPress

programmeradmin1浏览0评论

I need some help. I am trying to prevent direct access to a page that my customers get redirected to after checkout. I want the page to be accessible only after checkout.

I use the following code snippet to redirect users after checkout:

add_action( 'template_redirect', 'woocommerce_redirect_after_checkout' );

function woocommerce_redirect_after_checkout() {
global $wp;

if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {

$redirect_url = '/';

wp_redirect($redirect_url );

exit;
}

}

I have found this topic: prevent/block direct access to a thank you page

I placed the following code snippet to my functions.php to prevent direct access to the redirect page:

add_action('template_redirect', function() {
    // ID of the redirect page
    if (!is_page(2072)) {
        return;
    }

    // coming from the checkout, so all is fine
    if (wp_get_referer() === '/') {
        return;
    }

    // we are on thank you page
    // visitor is not coming from form
    // so redirect to home
    wp_redirect(get_home_url());
    exit;
} );

This works fine if the customer pays through Stripe. However, it does not work if the customer chooses to pay through PayPal because PayPal redirects the customer to their website to make the payment.

Can something be done here to fix this issue?

发布评论

评论列表(0)

  1. 暂无评论