Log Out Shortcode

Below, you’ll find a detailed guide on how to add the Log Out Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Log Out Shortcode Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Log Out Shortcode Plugin and the shortcodes it provides:

Plugin Icon
Log Out Shortcode

"Log Out Shortcode is a simple yet efficient WordPress plugin. It allows users to easily log out from the website by adding a shortcode. Perfect for enhancing user navigation and security."

★★★★★ (11) Active Installs: 4000+ Tested with: 5.5.13 PHP Version: false
Included Shortcodes:
  • [logout]

Log Out Shortcode [logout] Shortcode

The ‘logout’ shortcode from the Log-out-Shortcode plugin provides an efficient way to log out of WordPress. It generates a logout link, with customizable text and redirection path. The shortcode has three attributes: ‘text’, ‘redirect’, and ‘class’. ‘Text’ changes the logout link text, ‘redirect’ specifies the URL to redirect to after logging out, and ‘class’ adds CSS classes to the link. The shortcode checks if the user is logged in, and if not, it returns an empty string. If ‘logout_to_home’ or ‘logout_to_current’ is used, it sets the ‘redirect’ to ‘home’ or ‘current’ respectively. It then generates the logout link with the specified attributes.

Shortcode: [logout]

Parameters

Here is a list of all possible logout shortcode parameters and attributes:

  • text – Defines the text displayed on the logout link
  • redirect – Specifies the URL where users will be redirected after logging out
  • class – Allows adding CSS classes to the logout link for styling

Examples and Usage

Basic example – A simple logout link with default settings

[logout /]

Advanced examples

Logout link with custom text

[logout text="Sign out" /]

Logout link with redirect to home page

[logout redirect="home" /]

Logout link with redirect to current page

[logout redirect="current" /]

Logout link with custom CSS class

[logout class="custom-class" /]

Logout link with custom text, redirect to home page, and custom CSS class

[logout text="Sign out" redirect="home" class="custom-class" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [logout] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'logout', 'logout_shortcode' );

Shortcode PHP function:

function logout_shortcode( $atts, $content = '', $shortcode_name = '' ) {
	$defaults = array(
		'text'     => __( 'Log out' ), // This is the default log out text from wp-includes/general-template.php.
		'redirect' => '', // path/URL to redirect to after logging out.
		'class'    => 'logout', // CSS Class(es) to use in link.
	);

	if ( ! is_user_logged_in() ) {
		return '';
	}

	$atts = shortcode_atts( $defaults, $atts );

	if ( 'logout_to_home' === $shortcode_name ) {
		$atts['redirect'] = 'home';
	} elseif ( 'logout_to_current' === $shortcode_name ) {
		$atts['redirect'] = 'current';
	}

	if ( 'home' === $atts['redirect'] ) {
			$atts['redirect'] = home_url();
	} elseif ( 'current' === $atts['redirect'] ) {
		$atts['redirect'] = get_the_permalink();
	}

	$class_html = '';
	if ( '' !== $atts['class'] ) {
		// Multiple classes are separated by a space.
		$classes    = explode( ' ', $atts['class'] );
		$classes    = array_map( 'sanitize_html_class', $classes );
		$class_html = ' class="' . implode( ' ', $classes ) . '"';
	}

	return '<a href="' . esc_url( wp_logout_url( $atts['redirect'] ) ) . '"' . $class_html . '>' . esc_html( $atts['text'] ) . '</a>';

}

Code file location:

log-out-shortcode/log-out-shortcode/log-out-shortcode.php

Conclusion

Now that you’ve learned how to embed the Log Out Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *