// Hide admin toolbar on front-end from anyone without the ‘manage_options’ capability
add_filter(‘show_admin_bar’, function($bool){ return current_user_can(‘manage_options’) ? $bool : false; });

function redirect_to_login_page_from_anywhere() {

if ( is_user_logged_in() ) return;

if ( empty($_SERVER[‘REQUEST_URI’]) || ‘/’ === $_SERVER[‘REQUEST_URI’] ) {
$redirect = ”;
} else {
$redirect = ( is_ssl() ? ‘https://’ : ‘http://’ ) . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘REQUEST_URI’];
}
wp_redirect( wp_login_url( $redirect ) );
exit();
}
// add_action( ‘template_redirect’, ‘redirect_to_login_page_from_anywhere’ );

function send_users_to_home_page_on_login( $redirect_to, $request, $user ) {

// If the login page had a redirect param, proceed as usual
if ( $request ) return $redirect_to;
// If it’s an admin, proceed as usual
if ( ! empty( $user->allcaps[‘manage_options’] ) ) return $redirect_to;
// Otherwise, just a regular login, send to home instead of backend
return home_url();
}
add_filter( ‘login_redirect’, ‘send_users_to_home_page_on_login’, 10, 3 );

/**
* Restrict access to the administration screens.
*
* Only administrators will be allowed to access the admin screens,
* all other users will be automatically redirected to the front of
* the site instead.
*
* We do allow access for Ajax requests though, since these may be
* initiated from the front end of the site by non-admin users.
*/
function restrict_admin_with_redirect() {

if ( ! current_user_can( ‘manage_options’ ) && ( ! defined( ‘DOING_AJAX’ ) || ! DOING_AJAX ) ) {
wp_redirect( site_url() );
exit;
}
}
add_action( ‘admin_init’, ‘restrict_admin_with_redirect’, 1 );

/**
* Show a login directly on any page (doesnt redirect to wp-login)
*
* Only useful for a private site (or private areas, see examples)
*
* Returns them to the URL they were trying to access upon login.
*
* See wp_login_form() function for options.
*/
function login_on_any_page( $template ) {

// Exit this function if user is logged in.
if ( is_user_logged_in() ) return;

// if ( ‘members-only’ !== get_post_type() ) return;// Example: Restrict acces by post type
// if ( ! in_category( ‘members-only’ ) ) return;// Example: Restrict acces by category

?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<title><?php echo get_bloginfo( ‘name’, ‘display’ ) ?></title>
<meta name=”viewport” content=”width=device-width”>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<style>#login {width:320px;padding:8% 0 0;margin:auto;}</style>
<body>
<div id=”login”>
<h1 class=”site-title”><?php bloginfo( ‘name’ ); ?></h1>
<?php wp_login_form(); ?>
</div>
</html>
<?php

exit();
}
add_filter( ‘template_redirect’, ‘login_on_any_page’ );

// If you’re using a custom login page, this prevents failed login attempts from going to the standard login page
// add_action( ‘wp_login_failed’, function(){ wp_redirect( home_url(‘/some-custom-login-page/’) ); exit(); } );

add_shortcode( ‘if_logged_in’, function($a, $c = ”){ return (is_user_logged_in()) ? $c : ”; });
add_shortcode( ‘if_logged_out’, function($a, $c = ”){ return (!is_user_logged_in()) ? $c : ”; });

function wp_admin_bar_login_style() {

if ( is_user_logged_in() ) return;

?>
<style>
#readycat-login {
display: none;
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow-y: auto;
z-index: 100000;/* Just above WP Toolbar */
}
#readycat-login.login-state-hide {
display: none;
}
#readycat-login.login-state-show {
display: block;
}
#readycat-login-mask {
position: fixed;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.6);
}
#readycat-login-form {
margin: -22em auto 0;
position: relative;
box-sizing: border-box;
width: 30em;
max-width: 100%;
padding: 1em 2em;
background-color: #fff;
color: #000;
text-align: right;/* for register & forgot password links */
-webkit-transition: margin 0.3s ease-out;
transition: margin 0.3s ease-out;
}
#readycat-login.login-state-hide #readycat-login-form {
margin-top: -22em;
}
#readycat-login.login-state-show #readycat-login-form {
margin-top: 0;
}
#close-readycat-login {
position: absolute;
right: 1em;
top: 0.5em;
color: #000;
}
#readycat-login-form p {
margin: 0;
text-align: left;
}
#readycat-login-form p > input {
display: block;
width: 100%;
}
#readycat-login-form input#wp-submit {
margin: 1em 0;
}
</style>
<?php
}
add_action( ‘wp_head’, ‘wp_admin_bar_login_style’ );

function wp_admin_bar_login_script() {

if ( is_user_logged_in() ) return;
?>
<div id=”readycat-login”>
<div id=”readycat-login-mask”></div>
<div id=”readycat-login-form”>
<a id=”close-readycat-login” href=”/”>×</a>
<?php wp_login_form() ?>
</div>
</div>
<script>
document.addEventListener(‘DOMContentLoaded’, function(){
var triggers = document.querySelectorAll(‘.readycat-login-trigger’);
var len = triggers.length;
if(len){
function readycatLoginTrigger(e){
document.getElementById(‘readycat-login’).className = ‘login-state-show’;
e.preventDefault();
}
for ( i = 0; i < len; ++i ){
triggers[i].addEventListener(‘click’, readycatLoginTrigger );
}
document.getElementById(‘readycat-login-mask’).addEventListener(‘click’, function(e){
if(e.target === this)
document.getElementById(‘readycat-login’).className = ‘login-state-hide’;
});
document.getElementById(‘close-readycat-login’).addEventListener(‘click’, function(e){
document.getElementById(‘readycat-login’).className = ‘login-state-hide’;
e.preventDefault();
});
}
});
</script>
<?php
}
add_action( ‘wp_footer’, ‘wp_admin_bar_login_script’, 1 );

add_shortcode( ‘login_required’, function($a, $c = ”){ return (is_user_logged_in()) ? $c: ‘<style>#readycat-login{display:block}#readycat-login-form{margin-top:0}</style>’; });

// Show admin bar even when logged out
add_filter(‘show_admin_bar’, ‘__return_true’);

function wp_admin_bar_login_add( $wp_admin_bar ) {

if ( is_user_logged_in() ) return;

$wp_admin_bar->add_menu( array(
‘id’        => ‘login-trigger’,
‘parent’    => ‘top-secondary’,
‘title’        => ‘Login’,
‘href’        => wp_login_url(),
‘meta’        => array(
‘class’    => ‘frenchpress-login-trigger’,
‘html’ => ‘<style>div#wpadminbar li#wp-admin-bar-login-trigger {display: block;} div#wpadminbar li#wp-admin-bar-login-trigger > a {padding: 0 1em;}</style>’,
),
) );
}
add_action( ‘admin_bar_menu’, ‘wp_admin_bar_login_add’, 9999 );

function strip_admin_bar_of_all_but_user_actions( $wp_admin_bar ) {

if ( is_admin() || current_user_can(‘manage_options’) ) return;

$keep = array(// menu nodes to keep
‘top-secondary’,
‘my-account’,
‘user-actions’,
‘user-info’,
‘edit-profile’,
‘logout’,
‘search’,
);
$nodes = $wp_admin_bar->get_nodes();

$nodes = array_diff_key( $nodes, array_flip( $keep ) );

foreach ( $nodes as $id => $node ) {
$wp_admin_bar->remove_node( $id );
}
}
add_action( ‘admin_bar_menu’, ‘strip_admin_bar_of_all_but_user_actions’, 9999 );

Leave a Comment