Creating a Login-Protected Shortcode
In WordPress, you can create a custom shortcode to display content only to logged-in users. Add the following code to your theme's functions.php file or via a code snippets plugin.
/**
* Shortcode to display content only to logged-in users.
* @param array $atts Shortcode attributes.
* @param string|null $content The wrapped content.
* @return string The output for the user.
*/
function wp_members_only_shortcode( $atts, $content = null ) {
// Check if user is logged in, content is not empty, and it's not a feed.
if ( is_user_logged_in() && ! empty( $content ) && ! is_feed() ) {
return do_shortcode( $content ); // Return and parse inner shortcodes.
}
// Message for non-logged-in users.
return __( 'This content is for members only. Please log in to view.', 'your-text-domain' );
}
add_shortcode( 'members_only', 'wp_members_only_shortcode' );
Using the Shortcode to Hide Content
When creating a post or page, wrap the content you want to hide with the shortcode tags.
[members_only]
This is hidden content for logged-in users only. It can include text, images, links, etc.
[/members_only]
Result:
- Logged-in users: See the wrapped content normally.
- Non-logged-in visitors: See the default message: "This content is for members only. Please log in to view."
Adding a Shortcode Button to the Classic Editor
To easily insert the shortcode in the Classic Editor (Text mode), you can add a custom button.
Method: Add a TinyMCE Button via functions.php
Add the following code to your theme's functions.php file:
/**
* Add a custom shortcode button to the Classic Editor toolbar.
*/
function add_members_only_shortcode_button() {
// Check user permissions and editor page.
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
return;
}
if ( get_user_option( 'rich_editing' ) == 'true' ) {
add_filter( 'mce_external_plugins', 'add_members_only_tinymce_plugin' );
add_filter( 'mce_buttons', 'register_members_only_button' );
}
}
add_action( 'admin_init', 'add_members_only_shortcode_button' );
// Register the TinyMCE plugin script.
function add_members_only_tinymce_plugin( $plugin_array ) {
$plugin_array['members_only_shortcode'] = get_template_directory_uri() . '/js/tinymce-shortcode.js'; // Adjust path as needed.
return $plugin_array;
}
// Register the button in the toolbar.
function register_members_only_button( $buttons ) {
array_push( $buttons, 'members_only_shortcode' );
return $buttons;
}
Create the TinyMCE Button JavaScript File
Create a file named tinymce-shortcode.js in your theme directory (e.g., /js/) and add the following:
(function() {
tinymce.PluginManager.add( 'members_only_shortcode', function( editor, url ) {
editor.addButton( 'members_only_shortcode', {
title: 'Insert Members-Only Content',
icon: 'icon lock', // Or use a CSS class like 'dashicon-lock'.
onclick: function() {
// Insert the shortcode at the cursor.
editor.insertContent( '[members_only]nInsert members-only content here.n[/members_only]' );
}
});
});
})();
Note: This method works for the WordPress Classic Editor. For the Gutenberg block editor, creating a custom block is recommended for a better experience.
Advanced Tips and Considerations
- Security: This shortcode only controls front-end display; it does not protect attachments or files accessed via direct URLs.
- Cache Compatibility: If your site uses full-page caching (e.g., WP Rocket, W3 Total Cache), this feature may not work because cached pages show the same content to all users. Use it with your cache plugin's "exclude by user role" feature.
- Customizing the Message: You can customize the message via a shortcode attribute, e.g.,
[members_only message="Please log in to view the full tutorial."], and parse$atts['message']in the function. - Gutenberg Blocks: For a modern WordPress editing experience, consider developing a custom "Members Content" block for a more intuitive interface.