• I have wordpress installed on my site and I want to be able to use the logins from the wordpress to control the display of other things on the site (outside of the blog)

    I want users who are logged in to see a different menu and stuff like that.

    Is there a good way to do this? Check to see if users are logged in?

    currently, I’m including a bunch of wp files in every page to get this info. I’m afraid this is causing my processor problems.

    if($page_section != "news"){
    	$blog_location = $_SERVER['DOCUMENT_ROOT'] . "/news";
    	if (! isset($wp_did_header)):
    	if ( !file_exists( $blog_location . '/wp-config.php') ) {
    		if (strpos($_SERVER['PHP_SELF'], 'wp-admin') !== false) $path = '';
    		else $path = 'wp-admin/';
    
    		require_once( $blog_location . '/wp-includes/classes.php');
    		require_once( $blog_location . '/wp-includes/functions.php');
    		require_once( $blog_location . '/wp-includes/plugin.php');
    		wp_die("error message", "WordPress › Error");
    	}
    
    	$wp_did_header = true;
    
    	require_once( $blog_location . '/wp-config.php');
    
    	wp();
    
    	endif;
    
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter scotnery

    (@scotnery)

    bump

    You may use the function is_user_logged_in to test this.

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

    More about this function here: https://codex.www.ads-software.com/Function_Reference/is_user_logged_in

    actually on non wordpress sites you just need to include the wp-header

    <?php
    require('./wp-blog-header.php');
    ?>

    And then you can do what dragly said

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

    I found this post today as I wanted to do this myself and thought I’d be lazy and find out if someone had already done it. I don’t want to load wordpress for some URLs, so including the header was out (this bootstraps the whole wordpress installation anyway). This is what I came up with – it is not secure (as the cookies could easily be faked), but the stuff I want to hide isn’t mission critical (I use the blog for that!) – USE AT YOUR OWN RISK!

    $logged_in = false;
    // checks are performed on usernames ONLY
    $allowed_users = array("user1", "user2");
    if (count($_COOKIE)) {
        foreach ($_COOKIE as $key => $val) {
            if (substr($key, 0, 19) === "wordpress_logged_in") {
                if (preg_match('/^(' . implode('|', $allowed_users) . ')/', $val, $matches)) {
                    $logged_in = true;
                }
            }
        }
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to check if user is logged-in on non-wp pages?’ is closed to new replies.