Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Kim Lorimier
Kim Lorimier
6,818 Points

php function for logged in user

Hi all, I have been taking lots of classes here but am still new to php. I build mostly in wordpress.

I am trying to change an existing plugin to NOT show page content if a user is logged in.
I have found the is_user_logged_in function in the wordpress codex: example:

<?php
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}
?>

I want my logged in visitor to just see  a simple 
"You are currently logged in"

Here is the template I am trying to alter:

<?php global $rcp_options, $post; ?>


<?php if( ! is_user_logged_in() ) { ?>
    <h3 class="rcp_header">
        <?php echo apply_filters( 'rcp_registration_header_logged_in', __( 'Register New Account', 'rcp' ) ); ?>
    </h3>
<?php } else { ?>
    <h3 class="rcp_header">
        <?php echo apply_filters( 'rcp_registration_header_logged_out', __( 'You are currently a member', 'rcp' ) ); ?>
    </h3>
<?php }


// show any error messages after form submission
rcp_show_error_messages( 'register' ); ?>

<form id="rcp_registration_form" class="rcp_form" method="POST" action="<?php echo esc_url( rcp_get_current_url() ); ?>">

    <?php if( ! is_user_logged_in() ) { ?>

    <div class="rcp_login_link">
        <p><?php _e( sprintf( '<a href="%s">Log in</a> if you wish to renew an existing subscription.', rcp_get_login_url( rcp_get_current_url() ) ), 'rcp' ); ?></p>
    </div>

    <?php do_action( 'rcp_before_register_form_fields' ); ?>

    <fieldset class="rcp_user_fieldset">
        <p id="rcp_user_login_wrap">
            <label for="rcp_user_login"><?php echo apply_filters ( 'rcp_registration_username_label', __( 'Username', 'rcp' ) ); ?></label>
            <input name="rcp_user_login" id="rcp_user_login" class="required" type="text" <?php if( isset( $_POST['rcp_user_login'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_user_login'] ) . '"'; } ?>/>
        </p>
        <p id="rcp_user_email_wrap">
            <label for="rcp_user_email"><?php echo apply_filters ( 'rcp_registration_email_label', __( 'Email', 'rcp' ) ); ?></label>
            <input name="rcp_user_email" id="rcp_user_email" class="required" type="text" <?php if( isset( $_POST['rcp_user_email'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_user_email'] ) . '"'; } ?>/>
        </p>
        <p id="rcp_user_first_wrap">
            <label for="rcp_user_first"><?php echo apply_filters ( 'rcp_registration_firstname_label', __( 'First Name', 'rcp' ) ); ?></label>
            <input name="rcp_user_first" id="rcp_user_first" type="text" <?php if( isset( $_POST['rcp_user_first'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_user_first'] ) . '"'; } ?>/>
        </p>
        <p id="rcp_user_last_wrap">
            <label for="rcp_user_last"><?php echo apply_filters ( 'rcp_registration_lastname_label', __( 'Last Name', 'rcp' ) ); ?></label>
            <input name="rcp_user_last" id="rcp_user_last" type="text" <?php if( isset( $_POST['rcp_user_last'] ) ) { echo 'value="' . esc_attr( $_POST['rcp_user_last'] ) . '"'; } ?>/>
        </p>
        <p id="rcp_password_wrap">
            <label for="password"><?php echo apply_filters ( 'rcp_registration_password_label', __( 'Password', 'rcp' ) ); ?></label>
            <input name="rcp_user_pass" id="rcp_password" class="required" type="password"/>
        </p>
        <p id="rcp_password_again_wrap">
            <label for="password_again"><?php echo apply_filters ( 'rcp_registration_password_again_label', __( 'Password Again', 'rcp' ) ); ?></label>
            <input name="rcp_user_pass_confirm" id="rcp_password_again" class="required" type="password"/>
        </p>

        <?php do_action( 'rcp_after_password_registration_field' ); ?>

    </fieldset>
    <?php } ?>

    <?php do_action( 'rcp_before_subscription_form_fields' ); ?>

    <fieldset class="rcp_subscription_fieldset">
    <?php $levels = rcp_get_subscription_levels( 'active' );
    if( $levels ) : ?>
        <p class="rcp_subscription_message"><?php echo apply_filters ( 'rcp_registration_choose_subscription', __( 'Choose your subscription level', 'rcp' ) ); ?></p>
        <ul id="rcp_subscription_levels">
            <?php foreach( $levels as $key => $level ) : ?>
                <?php if( rcp_show_subscription_level( $level->id ) ) : ?>
                <li id="rcp_subscription_level_<?php echo $level->id; ?>" class="rcp_subscription_level">
                    <input type="radio" class="required rcp_level" <?php if( $key == 0 || ( isset( $_GET['level'] ) && $_GET['level'] == $key ) ) { echo 'checked="checked"'; } ?> name="rcp_level" rel="<?php echo esc_attr( $level->price ); ?>" value="<?php echo esc_attr( absint( $level->id ) ); ?>" <?php if( $level->duration == 0 ) { echo 'data-duration="forever"'; } ?>/>&nbsp;
                    <span class="rcp_subscription_level_name"><?php echo rcp_get_subscription_name( $level->id ); ?></span><span class="rcp_separator">&nbsp;-&nbsp;</span><span class="rcp_price" rel="<?php echo esc_attr( $level->price ); ?>"><?php echo $level->price > 0 ? rcp_currency_filter( $level->price ) : __( 'free', 'rcp' ); ?><span class="rcp_separator">&nbsp;-&nbsp;</span></span>
                    <span class="rcp_level_duration"><?php echo $level->duration > 0 ? $level->duration . '&nbsp;' . rcp_filter_duration_unit( $level->duration_unit, $level->duration ) : __( 'unlimited', 'rcp' ); ?></span>
                    <div class="rcp_level_description"> <?php echo rcp_get_subscription_description( $level->id ); ?></div>
                </li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
    <?php else : ?>
        <p><strong><?php _e( 'You have not created any subscription levels yet', 'rcp' ); ?></strong></p>
    <?php endif; ?>
    </fieldset>

    <?php if( rcp_has_discounts() ) : ?>
    <fieldset class="rcp_discounts_fieldset">
        <p id="rcp_discount_code_wrap">
            <label for="rcp_discount_code">
                <?php _e( 'Discount Code', 'rcp' ); ?>
                <span class="rcp_discount_valid" style="display: none;"> - <?php _e( 'Valid', 'rcp' ); ?></span>
                <span class="rcp_discount_invalid" style="display: none;"> - <?php _e( 'Invalid', 'rcp' ); ?></span>
            </label>
            <input type="text" id="rcp_discount_code" name="rcp_discount" class="rcp_discount_code" value=""/>
        </p>
    </fieldset>
    <?php endif; ?>

    <?php do_action( 'rcp_after_register_form_fields', $levels ); ?>

    <div class="rcp_gateway_fields">
        <?php
        $gateways = rcp_get_enabled_payment_gateways();
        if( count( $gateways ) > 1 ) : $display = rcp_has_paid_levels() ? '' : ' style="display: none;"'; ?>
            <fieldset class="rcp_gateways_fieldset">
                <p id="rcp_payment_gateways"<?php echo $display; ?>>
                    <select name="rcp_gateway" id="rcp_gateway">
                        <?php foreach( $gateways as $key => $gateway ) : $recurring = rcp_gateway_supports( $key, 'recurring' ) ? 'yes' : 'no'; ?>
                            <option value="<?php echo esc_attr( $key ); ?>" data-supports-recurring="<?php echo esc_attr( $recurring ); ?>"><?php echo esc_html( $gateway ); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <label for="rcp_gateway"><?php _e( 'Choose Your Payment Method', 'rcp' ); ?></label>
                </p>
            </fieldset>
        <?php else: ?>
            <?php foreach( $gateways as $key => $gateway ) : $recurring = rcp_gateway_supports( $key, 'recurring' ) ? 'yes' : 'no'; ?>
                <input type="hidden" name="rcp_gateway" value="<?php echo esc_attr( $key ); ?>" data-supports-recurring="<?php echo esc_attr( $recurring ); ?>"/>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <?php do_action( 'rcp_before_registration_submit_field', $levels ); ?>

    <p id="rcp_submit_wrap">
        <input type="hidden" name="rcp_register_nonce" value="<?php echo wp_create_nonce('rcp-register-nonce' ); ?>"/>
        <input type="submit" name="rcp_submit_registration" id="rcp_submit" value="<?php echo apply_filters ( 'rcp_registration_register_button', __( 'Register', 'rcp' ) ); ?>"/>
    </p>
</form>

Does anyone know what I need to replace and with what?

Thanks for your help!! Onward to more php classes....

Kim

Edited to format code.

2 Answers

You might look at taking this PHP track.

I don't know what is covered in the Wordpress track, but this might also be of great help to your with what you are trying to achieve.

// Your unregistered code means whatever code you want to run when an unregistered person is on that page.

is_user_logged_in() is a method called that probably returns true for logged in or false for not logged in. The ! means not. So the conditional statements are saying if person not logged in. When we suggest removing the !, we are changing the code to mean if the user is logged in. You can write your code in one of two ways. You can do an if (user is logged on) {do this} else {do that} or you can do the opposite: if (user not logged on) {do that} else {do this}

You can also chain if statements together like this if (condition 1) {do 1} elseif (condition 2) {do 2} elseif (condition 3) {do 3} else {do 4}. You have to figure out all the options you want to complete and the order that it makes sense for the program to executed them, then write the code.

Kim Lorimier
Kim Lorimier
6,818 Points

Thanks! Yes, it does help. The wordpress track covers enough php to do simple things and definitely recommends taking more php. Between actually building for clients and trying to prioritize my learning, I am constantly coming up against something I know a little (but not enough) about. I keep joking that this year my learning curve is like climbing the Himalayas.

Thanks for your help. You have definitely sent me in the right direction.

Kim

Glad to be of help

I think if you change this:

<?php 
    if( ! is_user_logged_in() ) { ?>
    <h3 class="rcp_header">
?>

// to

<?php
 <?php if( is_user_logged_in() ) { ?>
    <h3>You are logged in</h3>
<?php } else {
// Your unregistered code here
}
?>

I think you should accomplish what you want.

Kim Lorimier
Kim Lorimier
6,818 Points

HI, Thanks for your response. I am sorry to seem so obtuse. I have been trying to decipher this code for days. I had gotten to something like your description above but cannot figure out how to implement it- what do I replace or add on to. See my example below.

When I tried to add this at the top of the code, I just get "You are logged in" IN ADDITION to the form that I am actually want not to display.

There seems to be two places that use "is_user_logged_in" . One at the top like this

<?php if( ! is_user_logged_in() ) { ?>
    <h3 class="rcp_header">
        <?php echo apply_filters( 'rcp_registration_header_logged_in', __( 'Register New Account', 'rcp' ) ); ?>
    </h3>
<?php } else { ?>
    <h3 class="rcp_header">
        <?php echo apply_filters( 'rcp_registration_header_logged_out', __( 'You are currently a member', 'rcp' ) ); ?>
    </h3>
<?php }

The second in the middle like this:

    <?php if( ! is_user_logged_in() ) { ?>

    <div class="rcp_login_link">
        <p><?php _e( sprintf( '<a href="%s">Log in</a> if you wish to renew an existing subscription.', rcp_get_login_url( rcp_get_current_url() ) ), 'rcp' ); ?></p>
    </div>

    <?php do_action( 'rcp_before_register_form_fields' ); ?>

Last question: what does // your unregistered code here mean?

Thanks for all your help. I am really trying to understand. My php class level 1 gave me enough knowledge to be able to read the php and write simple statements, but not to understand more complex conditional statements. Onward. I know what classes come next!!!