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
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.
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.
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.
Clearly you're not reading the thread. I have an add-on installed that allows PHP to be parsed within pages and posts.
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: