Log in required for a listing type

Share this resource

Share on facebook
Share on twitter
Share on linkedin
Share on email
Share on facebook
Submitted by: Michael Shpigelman

Require a log in to view a listing type in this case its properties.

1. Go to /wp-admin/options-permalink.php and replace “listing” by %listing_type% so your URL will contain the listing type instead of doing a query on the DB to get the type of the listing.

2. Add the following code into your functions.php file.

 

add_action( 'wp_loaded', 'redirect_to_login' );
function redirect_to_login(){
    session_start();

    if(strpos($_SERVER['REQUEST_URI'], "properties") !== false && !is_user_logged_in()):
        $_SESSION['login_error_page'] = $_SERVER['REQUEST_URI'];
        wp_redirect(get_permalink(203));
        exit();
    endif;

    if($_SESSION['login_error_page']){
        wc_add_notice( __("Please register or login first to view properties.", 'my-listing'), 'error');
        echo "
        <script>
            window.onload = function() {
                jQuery(function(){
                    jQuery('.sign-in-form input[name="_wp_http_referer"]').val('" . $_SESSION['login_error_page'] . "');
                })
            }
        </script>";
        unset($_SESSION['login_error_page']);
    }
}
add_action( 'wp_loaded', 'redirect_to_login' );
function redirect_to_login(){
    session_start();

    if(strpos($_SERVER['REQUEST_URI'], "properties") !== false &amp;&amp; !is_user_logged_in()):
        $_SESSION['login_error_page'] = $_SERVER['REQUEST_URI'];
        wp_redirect(get_permalink(203));
        exit();
    endif;

    if($_SESSION['login_error_page']){
        wc_add_notice( __("Please register or login first to view properties.", 'my-listing'), 'error');
        echo "
        
            window.onload = function() {
                jQuery(function(){
                    jQuery('.sign-in-form input[name="_wp_http_referer"]').val('" . $_SESSION['login_error_page'] . "');
                })
            }
        ";
        unset($_SESSION['login_error_page']);
    }
}