• Resolved Will

    (@funkmasterbecks)


    Hi guys,

    I just wanted to give Groups a test-drive on a new website, but as it seems, the [groups_login] shortcode doesn’t render anything in WP4.8. Other shortcodes work as intended though.

    I also tried Groups on a fresh local WP install, with different themes and no third party plugins installed, but the login shortcode won’t render anyway.

    What am I missing?

    • This topic was modified 7 years, 9 months ago by Will.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Will, I’m another user but I have [groups_login] on a site running WP 4.8 and it is displaying and functioning as expected — so you might want to look for another cause. I’m running PHP 7.1 — have you checked your error logs or enabled debug? That might give a clue.

    Thread Starter Will

    (@funkmasterbecks)

    Looking at the code explained everything.

    @developers: it would be nice to add this functionality to the shortcode, even if it’s only for admins.

    The reason I couldn’t see the shortcode, is that it only renders if a user is logged out–and that includes admins. So as a logged in admin, you won’t see the output of the form. I understand that a logged in user should be redirected and not being displayed useless stuff, but when working on a website, I should be able to see the form at all times. It also should be mentioned in the documentation.

    To change the shortcode’s behavior, head over to the Groups plugins directory, and edit the file class-groups-shortcodes.php.

    Look for the following lines:

    /**
     * Renders the Groups login form.
     * 
     * The user is redirected to the current page after login by default.
     * The user can be redirected to a specific URL after login by
     * indicating the <code>redirect</code> attribute.
     *
     * @param array $atts
     * @param string $content
     * @return string the rendered form or empty
     */
    public static function groups_login( $atts, $content = null ) {
    	$current_url = ( is_ssl() ? 'https://' : 'https://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    	extract(
    		shortcode_atts(
    			array(
    				'redirect'        => $current_url,
    				'show_logout'     => 'no'
    			),
    			$atts
    		)
    	);
    	$redirect    = trim( $redirect );
    	$show_logout = trim( strtolower( $show_logout ) );
    	$output      = '';
    	if ( !is_user_logged_in() ) {
    		$output .= wp_login_form(
    			array(
    				'echo'     => false,
    				'redirect' => $redirect
    			)
    		);
    	} else {
    		if ( $show_logout == 'yes' ) {
    			$output .= self::groups_logout(
    				array(
    					'redirect' => $redirect
    				)
    			);
    		}
    	}
    	return $output;
    }

    Change it to:

    /**
     * Renders the Groups login form.
     * 
     * The user is redirected to the current page after login by default.
     * The user can be redirected to a specific URL after login by
     * indicating the <code>redirect</code> attribute.
     *
     * @param array $atts
     * @param string $content
     * @return string the rendered form or empty
     */
    public static function groups_login( $atts, $content = null ) {
    	$current_url = ( is_ssl() ? 'https://' : 'https://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    	extract(
    		shortcode_atts(
    			array(
    				'redirect'        => $current_url,
    				'show_logout'     => 'no'
    			),
    			$atts
    		)
    	);
    	$redirect    = trim( $redirect );
    	$show_logout = trim( strtolower( $show_logout ) );
    	$output      = '';
    	if ( !is_user_logged_in() || !is_admin() ) {
    		$output .= wp_login_form(
    			array(
    				'echo'     => false,
    				'redirect' => $redirect
    			)
    		);
    	} else {
    		//Facultative Code Start
    		global $current_user;
    		get_currentuserinfo();
    		echo '<span>You are currently logged in as: ' . $current_user->user_login . '</span><br>';
    		//Facultative Code End
    		if ( $show_logout == 'yes' ) {
    			$output .= self::groups_logout(
    				array(
    					'redirect' => $redirect
    				)
    			);
    		}
    	}
    	return $output;
    }

    Adding the condition if ( !is_user_logged_in() || !is_admin() ) {...}, is going to enable the rendering of the login form when logged in as an admin.

    Facultative Code
    Adding global $current_user; get_currentuserinfo(); echo '<span>You are currently logged in as: ' . $current_user->user_login . '</span>', is going to get the current user’s username, and render “You are currently logged in as: Username” in html. You can add some more attributes if you like (see WP Codex, or just leave it out and redirect the user automtically.

    Hope this helps.

    • This reply was modified 7 years, 9 months ago by Will.
    Thread Starter Will

    (@funkmasterbecks)

    //My initial answer just disappeared after editing it, so I’m going to post it again. If the original shows up again, feel free to delete this post.

    Looking at the code explained everything.

    @developers: it would be nice to add this functionality to the shortcode, even if it’s only for admins.

    The reason I couldn’t see the shortcode, is that it only renders if a user is logged out–and that includes admins. So as a logged in admin, you won’t see the output of the form. I understand that a logged in user should be redirected and not being displayed useless stuff, but when working on a website, I should be able to see the form at all times. It also should be mentioned in the documentation.

    To change the shortcode’s behavior, head over to the Groups plugins directory, and edit the file class-groups-shortcodes.php.

    Look for the following lines:

    /**
     * Renders the Groups login form.
     * 
     * The user is redirected to the current page after login by default.
     * The user can be redirected to a specific URL after login by
     * indicating the <code>redirect</code> attribute.
     *
     * @param array $atts
     * @param string $content
     * @return string the rendered form or empty
     */
    public static function groups_login( $atts, $content = null ) {
    	$current_url = ( is_ssl() ? 'https://' : 'https://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    	extract(
    		shortcode_atts(
    			array(
    				'redirect'        => $current_url,
    				'show_logout'     => 'no'
    			),
    			$atts
    		)
    	);
    	$redirect    = trim( $redirect );
    	$show_logout = trim( strtolower( $show_logout ) );
    	$output      = '';
    	if ( !is_user_logged_in() ) {
    		$output .= wp_login_form(
    			array(
    				'echo'     => false,
    				'redirect' => $redirect
    			)
    		);
    	} else {
    		if ( $show_logout == 'yes' ) {
    			$output .= self::groups_logout(
    				array(
    					'redirect' => $redirect
    				)
    			);
    		}
    	}
    	return $output;
    }

    Change it to:

    /**
     * Renders the Groups login form.
     * 
     * The user is redirected to the current page after login by default.
     * The user can be redirected to a specific URL after login by
     * indicating the <code>redirect</code> attribute.
     *
     * @param array $atts
     * @param string $content
     * @return string the rendered form or empty
     */
    public static function groups_login( $atts, $content = null ) {
    	$current_url = ( is_ssl() ? 'https://' : 'https://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    	extract(
    		shortcode_atts(
    			array(
    				'redirect'        => $current_url,
    				'show_logout'     => 'no'
    			),
    			$atts
    		)
    	);
    	$redirect    = trim( $redirect );
    	$show_logout = trim( strtolower( $show_logout ) );
    	$output      = '';
    	if ( !is_user_logged_in() || !is_admin() ) {
    		$output .= wp_login_form(
    			array(
    				'echo'     => false,
    				'redirect' => $redirect
    			)
    		);
    	} else {
    		//Facultative Code Start
    		global $current_user;
    		get_currentuserinfo();
    		echo '<span>You are currently logged in as: ' . $current_user->user_login . '</span><br>';
    		//Facultative Code End
    		if ( $show_logout == 'yes' ) {
    			$output .= self::groups_logout(
    				array(
    					'redirect' => $redirect
    				)
    			);
    		}
    	}
    	return $output;
    }

    Adding the condition if ( !is_user_logged_in() || !is_admin() ) {...}, is going to enable the rendering of the login form when logged in as an admin.

    Facultative Code
    Adding global $current_user; get_currentuserinfo(); echo '<span>You are currently logged in as: ' . $current_user->user_login . '</span>', is going to get the current user’s username, and render “You are currently logged in as: Username” in html. You can add some more attributes if you like (see WP Codex, or just leave it out and redirect the user automtically.

    Hope this helps.

    Will, thank you for sharing your code suggestion; I am sure it will be helpful to some developers. But keep in mind that the intended behavior of groups and various short codes is to display different content depending on the status of the user. It can get complicated when there are multiple groups, each with different types of targeted content.

    When I am working on my site I typically open another browser window to view in incognito mode … which is exactly what I did to verify that the login short code was working on my site when I saw your first post. I don’t want to see the login screen as admin, because there is other content on the page meant only for logged in users, and if multiple elements are displayed at once, it can impact overall page layout.

    Keep in mind that any changes you make to the plugin will be overridden whenever it is updated. If you are a sophisticated coder, you might want to consider whether you could achieve the same result with code or a filter added to your functions.php file, or even create your own add-on plugin.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Solved] Groups Login Shortcode in WP 4.8’ is closed to new replies.