How to make WordPress private using code – .com

In this tutorial we are going to see how to make our WordPress private easily and quickly, using a few lines of code.

Private WordPress?

As we know, WordPress offers, without the need to install any extras, the ability to create private pages and posts, accessible only to site administrators and editors, and also to create password-protected content, so that we can have public posts and pages and private.

But it is true that sometimes we have other needs and we need to go a little further in terms of the privacy of our content. In order to include extra functionalities in this sense, we published the tutorial where we saw how to allow users whose roles were different from those of administrator and editor to be able to access the private entries of our website, for which by default they do not have sufficient capabilities.

If you want to learn more about the roles, take a look at , there we see it in depth. But in this case we are going to see how to make our WordPress totally private. There are several occasions when we may need to keep our website private, even temporarily.

For example when we are developing the site, a period in which we do not want Google to index our page and we do not want any unauthorized user to access it.

In fact, in WordPress there is the possibility of modifying the visibility of our website in the face of search engines. This option is found in “Settings/Reading”, but it will be the search engines that decide if they take the request into account or not. In any case, this does not prevent a user who knows the url of the web from seeing it.

See also  Main search engines - .com

In this tutorial we are going to see how we could turn WordPress into a totally private site only accessible to users who are registered and logged into our site. Let’s say we could call it “intranet” although it would be very basic :).

If you want to develop an intranet with all its features, we recommend you take a look at where you will learn how to create private websites that allow users (students, workers, clients, etc.) access to specific information, documents and resources. With all that said, let’s get started!

Make WordPress private using code

As we have seen, there are cases in which we may be interested in restricting access to all our content and making a totally private site that is only accessible to users who log in.

Well, this option not available in WordPress core, but we have to add it expressly. So let’s see how we can include this functionality.

There are many plugins that allow it, but in this case we will do it directly with code since it is something very specific. Basically it is about adding a code to WordPress to force users to log in to our site in case they want to access the content.

Specifically, we will add a few lines of code so that every time a user who has not logged in accesses an address on our website, they will be redirected to the login page to identify themselves, it’s that simple.

We can add this code in various ways as we have seen other times. We could insert it into the functions.php file of the child theme that we have active, use the plugin that will facilitate the task, or one of the utilities that we have previously created. If you need more information about it, we recommend you visit the tutorial on .

See also  Promoted Tweets - .com

In this case we will choose to add the code in a utility plugin. For this we will have to access said plugin through the FTP program that we use. To avoid errors, the first thing to do is deactivate the plugin, add the code, and then activate the plugin again. The code would be similar to this:

// Redirect unidentified users to login page add_action( ‘template_redirect’, ‘bld_redirect_login_login’ ); function bld_redirect_logged_in() { if ( !is_user_logged_in() ) { wp_safe_redirect( wp_login_url( home_url(add_query_arg(array(), $wp->request)) ), 302 ); exit(); } }

As we see in the code, the first thing we do is add a custom function “bld_redirect_login_session” in the “template_redirect” action hook that is executed before WordPress decides which template to load. This line could also have been placed after said function, it doesn’t matter in this case.

Next we define the function “bld_redirect_start_session”, and that is where we check if the user who is accessing one of the urls of our page is logged in or not. In the event that it is, we will not do anything, we do not want to redirect you to any site, you will be able to access the content since you are an authorized user for it.

If, on the other hand, you are not identified, then we will redirect you to the login page, so that if you are a registered user on our website, you can log in and access the private content.

Finally, just comment that if we do not have the “Anyone can register” option activated, which is found in “Settings/General/Members”, we will have to register users manually, which will allow us greater control in terms of privacy is concerned. And that’s all! With this small piece of code we have managed to turn our website into a private site.

See also  WordPress security course #7: Sucuri - .com

Summary and conclusion

On some occasions we may need to turn our page into a totally private site only accessible to users who log in, and therefore are registered on our website.

In this tutorial we have seen how we can do this in a simple way through a code that redirects unidentified users to the login page so that they can log in and view the content of the web. In some cases you will have enough with this basic functionality, however if what you are looking for is something more complete you can take a look at where the subject is discussed in depth.

And you already know, if you want to know more about WordPress, we recommend the courses at , you will have access to the previous course and more than 6047 step-by-step guided videos 🙂

Loading Facebook Comments ...
Loading Disqus Comments ...