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. Function to return true if current page has child pages
  4. Cleaning up WordPress to improve performance?
  5. List of all theme customizer control types?
  6. Shortcode putting html such as
  7. How to preserve PHP modifications while upgrading WordPress?
  8. Counting number of posts in a category and its sub categories and displaying result using shortcode
  9. Shortcode created to check language not works
  10. Add/echo div with Analytics-Code to function in functions.php
  11. How to add default images into theme customizer image control?
  12. Custom plugin issue when trying to use the shortcode twice on a page [closed]
  13. How can I default to all WordPress roles when parameter is not included in shortcode?
  14. WordPress menu deletes when trying to add a hook
  15. Overriding a theme redux file in child theme
  16. Recent post display using shortcode
  17. Nested shortCode functions in the functions.php file
  18. Making Quote Plugin more efficient
  19. Shortcode content output but not in correct place
  20. is_page “range” for if statement?
  21. PHP code inside shortcodes
  22. Setting custom canonical urls
  23. Trigger popup in a php if/else statement
  24. How to output a PHP file values by shortcode?
  25. Converting HTML Template to WordPress Theme
  26. Login/logout in header
  27. Retrieve $_POST data submitted from external URL in WordPress(NOT API)
  28. How to pick the default selected value in wordpress dropdown?
  29. Pass php dynamic variable to shortcode
  30. Modify custom field input data before creating a new post
  31. Compare the old get_theme_mod($name) to the new get_theme_mod($name) return value
  32. How to Reference/Echo Variable from Another PHP Function
  33. Remove echo from shortcode
  34. Replace shortcode in substring
  35. Need help enqueueing webfonts
  36. Redirect to another page using contact form 7? [closed]
  37. Log in / Log Out Custom Button
  38. How to obtain the current website URL in my theme?
  39. What exactly do this function declared into functions.php file of a WP theme?
  40. How to display login form anywhere, when user isn’t logged in, without redirecting?
  41. Shortcode from a function not working
  42. Display Data in Table from External Database in WP using Shortcodes
  43. Is it possible to return content, and then also continue to do other things?
  44. Creating WordPress Shortcode with Variable
  45. Shortcode to find and replace URL
  46. Help with WordPress function inside a shortcode
  47. Function Reference Documenting Template Tags for use in Custom Theme Templates?
  48. Create page template via functions.php?
  49. How to create a field in customize and show that in header.php?
  50. Shortcode to embed Edit Account form not working
  51. Shortcode for Listing Users from Meta Value?
  52. List post by title length
  53. Need to Modify a WordPress Shortcode with another wordpress shortcode
  54. Shortcode to log user into current URL
  55. Create special button on WP Tiny MCE Posts Editor for Shortcodes
  56. AJAX function not working [closed]
  57. is_user_logged_in returning nothing on custom page
  58. Customize field names in backend profile edit page through function.php [duplicate]
  59. WP insert post Redirect after function has executed
  60. Display logged in user name and lastname on page
  61. Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
  62. Can’t log in to WordPress wp-admin after adding code to functions.php
  63. Display Visual Composer shortcode if a post belongs in specific categories
  64. Why is the current page loaded in the pop-up window and not the specified one?
  65. How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
  66. Block error message in foreach loop when looping through ACF field
  67. How to block specific user id in custom login form?
  68. How can I prevent a shortcode div from extending beyond its boundaries?
  69. PHP multiple forms, same page, isset($_POST[]) not working?
  70. Gravity Forms: How to add PHP function to confirmation conditional shortcode?
  71. wpdb->query returns different value to phpMyAdmin
  72. shortcodes, custom php and their errors
  73. Display a custom name when the user has no name settle in his account
  74. Restrict wordpress access to logged users only
  75. Display current user metadata on WordPress page
  76. Reload page with a different shortcode when a user selects from a dropdown
  77. Refresh page after login with litespeed cache
  78. How Can I Display the Last Modified Date for User Profiles on WordPress?
  79. Redirect after login depending on the URL
  80. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  81. Offset with ajax load more posts duplicates
  82. Get posts by id using shortcode
  83. Shortcode’s output to use as other shortcode’s parameter
  84. is_user_logged_in() not working in homepage
  85. Remove node from multisites?
  86. Automatically refresh page if widget is added to page?
  87. Add two or multiple functions in WordPress Post or Page
  88. Need help with PHP functions
  89. How do I routinely extract the thumbnail of the most recent post?
  90. A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
  91. Shortcoding with Divs
  92. How to add button to top of theme customizer?
  93. Display a list of users with avatar filterable with alphabets
  94. Whats wrong with my code? Need To add String to shortcode? [closed]
  95. My title is showing after the shortcode
  96. Shortcode to include PHP file, pass various parameters to include?
  97. How call WordPress shortcode inside function file
  98. Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
  99. How to change product title color in shop page if product has specific product tag (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
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 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