Skip to content

Instantly share code, notes, and snippets.

@karlgroves
Last active December 19, 2015 13:29
Show Gist options
  • Save karlgroves/5962696 to your computer and use it in GitHub Desktop.
Save karlgroves/5962696 to your computer and use it in GitHub Desktop.
This snippet allows you require a user be logged into Wordpress in order to access non-WP content. For instance, imagine you have another section of your site that isn't part of Wordpress but still requires the user to be authenticated. Using this code, you can share the Wordpress authentication for that part of the site as well. Add this snippe…
<?php
// If your Wordpress blog isn't in the document root,
// you may have to change the path to wherever the
// Wordpress root directory
require($_SERVER['DOCUMENT_ROOT'].'/wp-blog-header.php');
// if the user isn't logged in, this will redirect them
// to the login page. On successful login it will redirect
// them to the page this code is currently in.
if(FALSE === is_user_logged_in()){
wp_redirect(wp_login_url(site_url() . $_SERVER['REQUEST_URI']), 302);
exit;
}
echo 'WHEE!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment