Run Wordpress Functions (PHP) in Page (Check User Logged In)

Discussion in 'WordPress' started by brandon93s, Aug 13, 2012.

  1. #1
    I am trying to use the build in PHP functions to check wether or not a user is logged in. If they are logged in, I wish to show content A, otherwise I want to show content B.
    I have installed a plugin that will allow me to use PHP code in my wordpress posts and pages, so this aspect is taken care of. However, the functions aren't called/accessed...

    I did some trouble shooting and tried to include the containing php file directly, to no success. My code is below.

    
    
    <?php
     include 'wp-includes/pluggable.php'; 
     if ( is_user_logged_in() ) { 
               echo 'Welcome, registered user!';
     } else { 
               echo 'Welcome, visitor!'; } 
    ?>
    PHP:
    No luck :(
     
    brandon93s, Aug 13, 2012 IP
  2. brandon93s

    brandon93s Active Member

    Messages:
    798
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    The wordpress core is clearly already loaded since this is a wordpress page and not an external PHP page. So it should be able to call it if my PHP is executing correctly...? I think.

    UPDATE: Solved my own mistake. My plugin parses php tags automatically, so placing <?php ?> was completely unnecessary and it wasn't executing properly. Removed them, and everything is correct.
     
    brandon93s, Aug 13, 2012 IP
  3. brandon93s

    brandon93s Active Member

    Messages:
    798
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #3
    However, this still isn't going to work. I need to do more than just echo some text back from the console. I need to display full-fledged html and none of the php plug-ins are able to parse multi-section php codes in a wordpress page.

    
    <?php if (is_user_logged_in()) { ?>
    LOGGED IN
    <?php } ?>
    
    <?php if (!is_user_logged_in()) { ?>
    VISITOR
    <?php } ?>
    
    Code (markup):
    outputs both "logged in" and "visitor". The same occurs when I use an else or elseif instead of a negated if.
     
    brandon93s, Aug 13, 2012 IP
  4. brandon93s

    brandon93s Active Member

    Messages:
    798
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Glad you like it... still hoping to solve the issue with it though.
     
    brandon93s, Aug 14, 2012 IP
  5. Glock

    Glock Active Member

    Messages:
    274
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #5
    As far as I know PHP does not work when added to the editor. You need to place the PHP code into the more generic file like single.php maybe and you can place the content there.
     
    Glock, Aug 14, 2012 IP
  6. brandon93s

    brandon93s Active Member

    Messages:
    798
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #6
    Clearly you're not reading the thread. I have an add-on installed that allows PHP to be parsed within pages and posts.
     
    brandon93s, Aug 14, 2012 IP
  7. brandon93s

    brandon93s Active Member

    Messages:
    798
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #7
    After much chagrin, I have come up with the best method to accomplish what I was looking to do.

    Add this to the bottom of your functions.php file in wordpress:
    
    add_shortcode( 'member', 'member_check_shortcode' );
    
    function member_check_shortcode( $atts, $content = null ) {
    	 if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
    		return $content;
    	return '';
    }
    
    add_shortcode( 'visitor', 'visitor_check_shortcode' );
    
    function visitor_check_shortcode( $atts, $content = null ) {
    	 if ( !is_user_logged_in() && !is_null( $content ) && !is_feed() )
    		return $content;
    	return '';
    }
    
    PHP:
    This will then be the content in your posts. Include the "logged in" information in [member] and the not-logged in information in [visitor].
    
    [member]
    I'm logged in!
    [/member]
    
    [visitor]
    I'm not logged in!
    [/visitor]
    PHP:
     
    brandon93s, Aug 14, 2012 IP