• Resolved albern

    (@albern)


    Hi, I’m wondering how could I modify the template so user has to register or login (if already registered) before he can see the website content.
    I mean to display WP standard login or register form. I used next code:
    <?php
    /*
    Template Name: Logged-In Users Page
    */
    ?>

    <?php if(is_user_logged_in()):?>
    Next on the very bottom of the file:

    <?php else:
    wp_die(‘Sorry, you must first log in to view this page. You can register free here.’);
    endif;?>

    but this is redirection and besides, the register action does not seem to be working.

    When I use header(/wp-login.php) I’m getting arrors about ‘header already send’.

    Help pls.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Just change the code that comes up if there are no posts, and add your condition to ‘the loop’, the have_posts() call, no need for the ‘wp_die’.

    Like this untested code which has a link to the login page, the login dialog has the ‘register here’ link there already!

    If the user is not ‘logged in’ they do not see the content and get the custom message, if the user is ‘logged in’ they get the second message if there is no content!

    <?php if ( is_user_logged_in() && have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<!-- Post Output -->
    	<?php endwhile; ?>
    <?php else : ?>
    	<?php if ( !is_user_logged_in() ) : ?>
    		<p><?php _e( 'You must be logged in to view this page. <a href="https://mysite.com/login">Login Page</a>', 'namespace' ); ?></p>
    	<?php else : ?>
    		<p><?php _e( 'Apologies, but no results were found', 'namespace' ); ?></p>
    	<?php endif; ?>
    <?php endif; ?>

    HTH

    David

    Thread Starter albern

    (@albern)

    Thank you for the answer. I’m affraid, you did not get me correctly.
    I need the WP login/register form to be the first page ever user arrive when he comes to the website. Just like when you come to the admin login area.
    I tried to use redirection: header(“/wp-login.php”), but I’m getting an error: headers already sent by

    Ok I see try wp_login_form() and wp_register(), have a look at the wp_login_form argument defaults.

    Note wp_register() will only show the link if you have anyone can register set in settings, the form when logged in will return to the page!

    <?php if( !is_user_logged_in() ): ?>
           <!-- Login Form for new or not logged in users -->
    	<?php wp_login_form(); ?>
    	Not registered then <?php wp_register( '','' ); ?> here!
    <?php else: ?>
       <!-- Normal template code for logged in users -->
    
    <?php endif; ?>

    HTH

    David

    Thread Starter albern

    (@albern)

    Thank you.

    No problem,
    Can you mark this topic as resolved please.

    David

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to get user to register or login’ is closed to new replies.