Discover in this tutorial how to display the content of a page only to logged in users using code.
In some cases you may be interested in displaying the content of a page only to users who are logged in your website, but not to visitors.
If it is a website with a membership you can use the same membership plugin to restrict that content. But if this is not the case and you want, for example, to hide the page where you have added the documentation for customers here I explain a very simple way to do it without plugins.
Let’s see how.
Snippet to hide the content to non-logged in users
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this case, you use the filter the_content
in combination with WordPress conditional tags so that when visiting a page a message is displayed, instead of the content, if the user is not logged in.
To adapt it to your needs you just have to change the page identifier (in this case 384) and customize the message you want to be displayed.
You can get the page ID by checking the URL when you edit the page or, if you want to make it easier for yourself, get it displayed directly in the WordPress admin with this tutorial.
Conclusion
With a few lines of code you can get the content of a page to be displayed only to logged in users and return a custom message to those who are not logged in.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉