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:
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:
- WP Custom tables query
- Woocomerce custom add to cart button edit functionality [closed]
- How to manually fix the WordPress gallery code using PHP in functions.php?
- Function to return true if current page has child pages
- Cleaning up WordPress to improve performance?
- List of all theme customizer control types?
- Shortcode putting html such as
- How to preserve PHP modifications while upgrading WordPress?
- Change the footer text on the login page
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Shortcode created to check language not works
- Change comments form title on a page by page basis
- Setting up the child theme so as to enable right-to-left WordPress?
- Add/echo div with Analytics-Code to function in functions.php
- delete an array element when its date expires
- How to add default images into theme customizer image control?
- Custom plugin issue when trying to use the shortcode twice on a page [closed]
- How can I default to all WordPress roles when parameter is not included in shortcode?
- WordPress menu deletes when trying to add a hook
- Overriding a theme redux file in child theme
- Wrap each shortcode in array to div
- Recent post display using shortcode
- How to break down importing of feeds
- Nested shortCode functions in the functions.php file
- Making Quote Plugin more efficient
- Shortcode to find and replace URL
- Help with WordPress function inside a shortcode
- Function Reference Documenting Template Tags for use in Custom Theme Templates?
- calling a custom field value inside functions.php
- Create page template via functions.php?
- How to create a field in customize and show that in header.php?
- List all blogs, but exclude the main site
- Shortcode to embed Edit Account form not working
- Shortcode for Listing Users from Meta Value?
- Cant display an image via PHP in wordpress
- List post by title length
- Need to Modify a WordPress Shortcode with another wordpress shortcode
- How to return a string that has a jQuery and Ajax inside in a shortcode?
- Shortcode to log user into current URL
- How do i wrap woocomerce attribute in list?
- get_theme_mod outputs number when using WP_Customize_Cropped_Image_Control
- PHP Helper Class to create shortcodes
- Is there a hack for using is_page() within the function.php file?
- Create special button on WP Tiny MCE Posts Editor for Shortcodes
- AJAX function not working [closed]
- is_user_logged_in returning nothing on custom page
- Customize field names in backend profile edit page through function.php [duplicate]
- WP insert post Redirect after function has executed
- Display a custom name when the user has no name settle in his account
- Delete post meta by serialized meta value
- Restrict wordpress access to logged users only
- Menu not updating for logged in users after redirect
- how to use auth_redirect() redirect visitor to login page if they are not login when they click account and order page?
- Display current user metadata on WordPress page
- Reload page with a different shortcode when a user selects from a dropdown
- Refresh page after login with litespeed cache
- How Can I Display the Last Modified Date for User Profiles on WordPress?
- Redirect after login depending on the URL
- Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
- Redirecting the lost password page request when using a custon login page
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Offset with ajax load more posts duplicates
- Get posts by id using shortcode
- Shortcode’s output to use as other shortcode’s parameter
- is_user_logged_in() not working in homepage
- Remove node from multisites?
- Automatically refresh page if widget is added to page?
- Open/closed function [closed]
- Need to Echo A Url path to show on a wordpress page
- Add two or multiple functions in WordPress Post or Page
- Wrapping shortcode content in a span or link
- Need help with PHP functions
- Where can I find the declaration of `$_wp_theme_features`?
- How do I routinely extract the thumbnail of the most recent post?
- A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
- My customizer’s setting doesn’t set to the default and needed to click the control’s “Default” button before it’ll be set
- How to have Function of a plugin using global vars into a shortcode?
- Check if values exists DB
- Overwriting a Shortcode
- Shortcoding with Divs
- WordPress causing all code to be displayed on line 1. Receiving multiple errors after cleaning cookies and cache
- How to add button to top of theme customizer?
- How can I clear the theme mod settings?
- Display a list of users with avatar filterable with alphabets
- Whats wrong with my code? Need To add String to shortcode? [closed]
- Select area and checkbox data is not saving?
- My title is showing after the shortcode
- A next page function with shortcode?
- Shortcode to include PHP file, pass various parameters to include?
- Display terms on product page with shotrcode
- Add multiple HTML attributes to an Elementor button
- How call WordPress shortcode inside function file
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- Calling a function via a shortcode in javascript
- Adding a css class to the gallery
- Strip and print only the numbers found in current’s post excerpt (even if they are without space)
- How to change product title color in shop page if product has specific product tag (Woocommerce)?
- How to capture number input from wordpress form into acf field in woocommerce
- Restricting page by user role