I have added the following code to my functions.php to validate if a user is logged in before allowing them to fill out a form:

function my_members_only_shortcode($atts, $content = null)

    if (is_user_logged_in()) {
        $content = do_shortcode($content);
    } else {
        $content = 'This content is for members only, please log in or register first.';

    return $content;
add_shortcode('members_only', 'my_members_only_shortcode');

The above code has enabled me to use [members_only][iphorm id="1" name="Example form"][/members_only] on my posts to prevent users from accessing forms if they are not logged in -

However, I would like the [Members_only] function / short code to either display a clickable hyperlink to the wp-login.php page, so a user can login and register with ease at that point in time - or, if there was a way for the [members_only] function to automatically place a widget in the "not logged in" area under the not logged in message - I would want that to be the social medial login widget, which I can call up in a post by using a short code created by a plugin - [do_widget "Social Login"]

1 Answer 1


Have you tried:

function my_members_only_shortcode($atts, $content = null) {
    if ( !is_user_logged_in() ) {
        $content = '<p>This content is for members only. Please log in or register first.</p>' . do_shortcode('[do_widget "Social Login" ]');
    return $content;

That should replace the content with your message and special short code content only if the user is not logged in.

The short code looks funky to me ... so correct it if I have it wrong, but the idea is to just replace the content and use do_shortcode('[your_shortcode_here]') to append the form to your custom message... and leave the content alone if they are logged in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.