Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Having trouble creating two shortcodes, one for logged in user and one for visitors

The code exactly as you put it in your question works fine for me and does exactly what you suggested.

To make it work I had to make sure my WP post editor was in code editor mode as HTML or Visual Editor may create extra problems:

enter image description here

And this is what my test code in the post editor looked like, when I had a successful test:

<!-- wp:html -->
<p>this is the content</p>

<p>more content 
[member_only]You are logged in.[/member_only]
[visitor_only]Login / Register[/visitor_only]
</p>

<!-- /wp:html -->

You can make the second shortcode a bit clearer to read by adding ! for ‘not’ on the front of the first condition, then the structure is the same as the first shortcode, and you don’t need the else:

function visitor_only_shortcode($atts, $content = null)
{
    if (!is_user_logged_in() && !is_null($content) && !is_feed()) {
       return $content;
    }
}

add_shortcode('visitor_only', 'visitor_only_shortcode');

Related Posts:

  1. WP Custom tables query
  2. Woocomerce custom add to cart button edit functionality [closed]
  3. How to manually fix the WordPress gallery code using PHP in functions.php?
  4. Function to return true if current page has child pages
  5. Cleaning up WordPress to improve performance?
  6. List of all theme customizer control types?
  7. Shortcode putting html such as
  8. How to preserve PHP modifications while upgrading WordPress?
  9. Change the footer text on the login page
  10. Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
  11. Counting number of posts in a category and its sub categories and displaying result using shortcode
  12. Shortcode created to check language not works
  13. Change comments form title on a page by page basis
  14. Setting up the child theme so as to enable right-to-left WordPress?
  15. Add/echo div with Analytics-Code to function in functions.php
  16. delete an array element when its date expires
  17. How to add default images into theme customizer image control?
  18. Custom plugin issue when trying to use the shortcode twice on a page [closed]
  19. How can I default to all WordPress roles when parameter is not included in shortcode?
  20. WordPress menu deletes when trying to add a hook
  21. Overriding a theme redux file in child theme
  22. Wrap each shortcode in array to div
  23. Recent post display using shortcode
  24. How to break down importing of feeds
  25. Nested shortCode functions in the functions.php file
  26. Making Quote Plugin more efficient
  27. Shortcode to find and replace URL
  28. Help with WordPress function inside a shortcode
  29. Function Reference Documenting Template Tags for use in Custom Theme Templates?
  30. calling a custom field value inside functions.php
  31. Create page template via functions.php?
  32. How to create a field in customize and show that in header.php?
  33. List all blogs, but exclude the main site
  34. Shortcode to embed Edit Account form not working
  35. Shortcode for Listing Users from Meta Value?
  36. Cant display an image via PHP in wordpress
  37. List post by title length
  38. Need to Modify a WordPress Shortcode with another wordpress shortcode
  39. How to return a string that has a jQuery and Ajax inside in a shortcode?
  40. Shortcode to log user into current URL
  41. How do i wrap woocomerce attribute in list?
  42. get_theme_mod outputs number when using WP_Customize_Cropped_Image_Control
  43. PHP Helper Class to create shortcodes
  44. Is there a hack for using is_page() within the function.php file?
  45. Create special button on WP Tiny MCE Posts Editor for Shortcodes
  46. AJAX function not working [closed]
  47. is_user_logged_in returning nothing on custom page
  48. Customize field names in backend profile edit page through function.php [duplicate]
  49. WP insert post Redirect after function has executed
  50. Display a custom name when the user has no name settle in his account
  51. Delete post meta by serialized meta value
  52. Restrict wordpress access to logged users only
  53. Menu not updating for logged in users after redirect
  54. how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
  55. Display current user metadata on WordPress page
  56. Reload page with a different shortcode when a user selects from a dropdown
  57. Refresh page after login with litespeed cache
  58. How Can I Display the Last Modified Date for User Profiles on WordPress?
  59. Redirect after login depending on the URL
  60. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  61. Redirecting the lost password page request when using a custon login page
  62. PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
  63. Offset with ajax load more posts duplicates
  64. Get posts by id using shortcode
  65. Shortcode’s output to use as other shortcode’s parameter
  66. is_user_logged_in() not working in homepage
  67. Remove node from multisites?
  68. Automatically refresh page if widget is added to page?
  69. Open/closed function [closed]
  70. Need to Echo A Url path to show on a wordpress page
  71. Add two or multiple functions in WordPress Post or Page
  72. Wrapping shortcode content in a span or link
  73. Need help with PHP functions
  74. Where can I find the declaration of `$_wp_theme_features`?
  75. How do I routinely extract the thumbnail of the most recent post?
  76. A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
  77. My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
  78. How to have Function of a plugin using global vars into a shortcode?
  79. Check if values exists DB
  80. Overwriting a Shortcode
  81. Shortcoding with Divs
  82. WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
  83. How to add button to top of theme customizer?
  84. How can I clear the theme mod settings?
  85. Display a list of users with avatar filterable with alphabets
  86. Whats wrong with my code? Need To add String to shortcode? [closed]
  87. Select area and checkbox data is not saving?
  88. My title is showing after the shortcode
  89. A next page function with shortcode?
  90. Shortcode to include PHP file, pass various parameters to include?
  91. Display terms on product page with shotrcode
  92. Add multiple HTML attributes to an Elementor button
  93. How call WordPress shortcode inside function file
  94. Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
  95. Calling a function via a shortcode in javascript
  96. Adding a css class to the gallery
  97. Strip and print only the numbers found in current’s post excerpt (even if they are without space)
  98. How to change product title color in shop page if product has specific product tag (Woocommerce)?
  99. How to capture number input from wordpress form into acf field in woocommerce
  100. Restricting page by user role
Categories PHP Tags customization, functions, login, php, shortcode
How to remove media by filename?
Go back to the exact post page number

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress