A fresh mix of code snippets and tutorials

Require user login for any plugin

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:

// require login for chat plugin
add_shortcode('wpmix_require_login', 'wpmix_require_login');
function wpmix_require_login() {
	if (is_user_logged_in()) {
		echo do_shortcode('[your-plugin-shortcode]');
	} else {
		return "Login required for chat.";

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.

Related Posts

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress