WordPress Code Snippets

A fresh mix of code snippets and tutorials

Require user login for any plugin

84

Here’s a quick trick to require user-login for any plugin. For example, if you want users to be able to use your chat plugin only if they are logged in. Here’s how..

Just add this code to your theme’s functions.php file:

Change “your-plugin-shortcode” to the name of the shortcode for your plugin, or if you’re using a template tag instead, replace this:

echo do_shortcode('[your-plugin-shortcode]');

..with this:

return your_plugin_template_tag();

You may also want to customize the message that’s displayed (“Login required for chat.”) when visitors aren’t logged in. Notice the difference between echoing (for the shortcode) and returning (for the template tag) — an important distinction due to the way shortcodes work.

Learn more

Digging into WordPress