Skip to content

Instantly share code, notes, and snippets.

@SalvatoreNoschese
Last active December 2, 2023 13:44
Show Gist options
  • Save SalvatoreNoschese/224f8cc3571a426ecc0116f3ea3ca935 to your computer and use it in GitHub Desktop.
Save SalvatoreNoschese/224f8cc3571a426ecc0116f3ea3ca935 to your computer and use it in GitHub Desktop.
Custom Login Widget (mu-plugin)
<?php
/*
Plugin Name: Custom Login Widget
Description: Just add [custom_login_widget] in sidebar shortcode
Author: Salvatore Noschese
Author URI: https://salvatorenoschese.it
Version: 1.0
*/
defined( 'ABSPATH' ) || exit;
// Add shortcode
add_shortcode('custom_login_widget', 'custom_login_widget_function');
// Custom login widget function
function custom_login_widget_function() {
// Fix redirect if homepage (do not edit)
$clw_redirect = is_home() ? home_url('/') : get_permalink();
// Logged-in users
if (is_user_logged_in()) {
// User info
$clw_user = wp_get_current_user();
$clw_avatar = get_avatar($clw_user->ID, 64);
// User Output
$output = "<h3>" . sprintf(__('Howdy, %s'), $clw_user->display_name) . " &#128522;</h3>";
$output .= "<div class='user-profile-widget'>";
$output .= "<p style='float:left;margin:0 10px 0 0'><a href='" . esc_url(admin_url('profile.php')) . "' title='" . esc_attr__('Edit Profile') . "'>" . $clw_avatar . "</a></p>";
$output .= "<p><a href='" . esc_url(admin_url('profile.php')) . "' title='" . esc_attr__('Edit Profile') . "'>" . esc_html__('Edit Profile') . "</a></p>";
$output .= "<p>" . wp_loginout($clw_redirect, false) . "</p>";
$output .= "</div>";
} else {
// Register Link only if 'user can register' is enabled (else show an alert)
$clw_registerlink = get_option('users_can_register') ? wp_register(" | ", " | ", false) : " | ";
$clw_regdisabled = get_option('users_can_register') ? "" : "<em style='display:block;font-size:small'>(" . esc_attr__("Registration has been disabled.") . ")</em>";
// Guest Output
$output = "<h3>" . esc_attr__('User Login Name') . " &#x1F464;</h3>";
$output .= "<p style='font-size:medium'>";
$output .= wp_loginout($clw_redirect, false);
$output .= $clw_registerlink;
$output .= "<a href='" . esc_url(wp_lostpassword_url()) . "' title='" . esc_attr__('Lost your password?') . "'>" . esc_html__('Lost your password?') . "</a>";
$output .= $clw_regdisabled;
$output .= "</p>";
}
// After (separator)
$output .= "<hr />";
// Print code
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment