The first thing you do when you entered into your WordPress wp-admin page is try to login but do you know that it happens often that your WordPress website becomes a victim hundreds of times if its wp-admin area is not secured in one-to-many ways. Idea is simple, better secure it. It’s not always essential to use plugins for doing the same but in turn you can also use snippets – the short codes – which are basically means for login purpose. With this article, you will find below the 15 most essential WordPress login snippets.

wordpress-code-is-poetry

Expire Protected Post Cookie With Session

You need to set the time of 0 in setcookie() to your theme’s functions.php template. Setting the time will let you forces the cookie to expire the session.

Source: Drew Jaynes

Display login form anywhere with wp_login_form

Sometimes it happens that you may wish to add a login form to customize your WordPress theme and this can be possible by adding wp_login_form() function to your theme. All you need to do is to submit the following snippet in the location where you wish to display the login form.

Login with username or email address

The snippet will let users login using a username or an email address. You need to put the snippet to the functions.php of your WordPress theme. The snippet will change the text on the login page from “username” to “username/email.”

Source: c.bavota

Display avatar for currently logged in user with get_avatar

The snippet will let you display the currently logged in users avatar in your theme.

Redirect based on user roles or capabilities

The snippet will let you redirect users based on roles or capabilities. You need to add the snippet to the functions.php template of your WordPress theme.

Source: Milo via Stackexchange

Redirect back to referring page after login

Adding this snippet to the functions.php of your WordPress theme will redirect your visitors to the page they were viewing after logging in.

User controlled optional login redirect

The snippet will let user controlled optional login redirect with a select menu.

Source: Taproot Creative

Add a login link to the wp_nav_menu

The snippet will add a login link to your wp_nav_menu.

Require login to view posts

The code below will make a condition in front of your readers to login first if they want to see specific posts defined by and array of post IDS. The code required to be added to the functions.php template

Source: New2WP

Prevent access to wp-admin

Adding this snippet to your functions.php will redirect subscribers away from wp-admin to the home page.

Source: Flowdrops.com

Extend auto logout period

The code will extend the length of time cookies.

Check if user is logged in

The code will let you display alternate content to users that are logged in.

htaccess for a better login URL

The code will change your login url from www.website.com/wp-login.php to www.website.com/login/

Sidebar login

Adding the snippet to the sidebar of your WordPress theme will let you allow your visitors to register and login from your sidebar.

Hide login errors

The code will disable the login errors that are displayed to users.