Disable Direct Access to A WordPress Page

Share Now

<?php
add_action('template_redirect', function () {
    //Page you want to redirect
    if (!is_page(ADD_YOUR_PAGE_ID)) {
        return;
    }
    if (wp_get_referer() == 'ENTER_THE_URL_FROM_WHERE_IT_SHOULD_BE_ACCESSIBLE') {
        return;
    }
    //Where You Want To Redirect
    wp_redirect(get_permalink(ADD_YOUR_PAGE_ID));
    exit;
});
?>
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert