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
- 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?
- 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
- 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
- Making Quote Plugin more efficient
- Placing raw HTML inside a WordPress shortcode
- PHP can I add line numbers to file_get_contents()
- Shortcode content output but not in correct place
- PHP code inside shortcodes
- Shortcode content filter?
- Setting custom canonical urls
- Trigger popup in a php if/else statement
- How to output a PHP file values by shortcode?
- How to display posts by current user in a drop down
- Get value from shortcode to do something
- Retrieve $_POST data submitted from external URL in WordPress(NOT API)
- How to pick the default selected value in wordpress dropdown?
- Custom shortcode outputs plain text instead of HTML at top of post
- Pass php dynamic variable to shortcode
- Modify custom field input data before creating a new post
- Compare the old get_theme_mod($name) to the new get_theme_mod($name) return value
- Remove echo from shortcode
- Need help enqueueing webfonts
- Redirect to another page using contact form 7? [closed]
- Log in / Log Out Custom Button
- Shortcode with PHP issue “Undefined index”
- How to obtain the current website URL in my theme?
- How to display login form anywhere, when user isn’t logged in, without redirecting?
- Shortcode from a function not working
- Is it possible to return content, and then also continue to do other things?
- ::before on open/close function [closed]
- Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
- Creating WordPress Shortcode with Variable
- 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?
- Shortcode to embed Edit Account form not working
- Shortcode for Listing Users from Meta Value?
- Cant display an image via PHP in wordpress
- Need to Modify a WordPress Shortcode with another wordpress shortcode
- Shortcode to log user into current URL
- get_theme_mod outputs number when using WP_Customize_Cropped_Image_Control
- 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]
- Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
- Can’t log in to WordPress wp-admin after adding code to functions.php
- Display Visual Composer shortcode if a post belongs in specific categories
- Why is the current page loaded in the pop-up window and not the specified one?
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Block error message in foreach loop when looping through ACF field
- How to block specific user id in custom login form?
- update_user_meta as multiple value but with same meta key
- How can I prevent a shortcode div from extending beyond its boundaries?
- PHP multiple forms, same page, isset($_POST[]) not working?
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- shortcodes, custom php and their errors
- Display a custom name when the user has no name settle in his account
- Restrict wordpress access to logged users only
- 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)
- Offset with ajax load more posts duplicates
- Get posts by id using shortcode
- Shortcode’s output to use as other shortcode’s parameter
- Remove node from multisites?
- Automatically refresh page if widget is added to page?
- Add two or multiple functions in WordPress Post or Page
- Need help with PHP functions
- A Customizer checkbox control that sets the setting to “” or to “blank” and show() or hide() a color control
- 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?
- Display a list of users with avatar filterable with alphabets
- My title is showing after the shortcode
- Shortcode to include PHP file, pass various parameters to include?
- How call WordPress shortcode inside function file
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)