Categories
How To

Sidebar Login Widget

Simple widget with basic options for a login form.

So this happened after people asked me about how to make a widget. This makes a slightly customizable widget that will let you have a slightly customized login form via a widget.

As with all hacks, you’re on your own for support. Code is GPLv2, and it should be translatable.

Download Code Download Code

The code is in a .txt file. Rename if .php and toss it in mu-plugins

Download TXT

Top ↑

Code Code

.

Sidebar Login Widget is released WITHOUT any support, and you can
use it at your own risk. The code should work, but I’m not spending
a lot of time on it.
*/

if (!defined(‘ABSPATH’)) {
die();
}

// Register the widget
function sli_register_widgets() {
register_widget( ‘sli_SLI_widget’ );
}
add_action( ‘widgets_init’, ‘sli_register_widgets’ );

// This is the widget!
class sli_SLI_widget extends WP_Widget {

function sli_SLI_widget() {
$widget_ops = array( ‘classname’ => ‘sli_add_users’, ‘description’ => ‘Sidebar Login Form.’ );
$control_ops = array( ‘width’ => 300, ‘height’ => 350, ‘id_base’ => ‘sli-add-user-widget’ );
$this->WP_Widget( ‘sli-add-user-widget’, ‘Sidebar Login’, $widget_ops, $control_ops );
}

function widget( $args, $instance ) {
extract( $args );

/* User-selected settings. */
$title = apply_filters(‘widget_title’, $instance[‘title’] );
$errormsg = $instance[‘errormsg’];
$lostmsg = $instance[‘lostmsg’];
$show_for = $instance[‘show_for’];
$show_reg = $instance[‘show_reg’];

/* Before widget (defined by themes). */
echo $before_widget;

/* Title of widget (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;

// Code Goes Here

global $user_login, $post;
$redirect = $_SERVER[‘REQUEST_URI’];

if (isset($_GET[‘sli’])) {
$sli = $_GET[‘sli’];
if ($sli == ‘fail’) { echo “

“. $errormsg .”

“; }
if ($sli == ‘lost’) { echo “

“. $lostmsg .”

“; }
}

if (is_user_logged_in()) {
_e( ‘Howdy’, ‘helfsli’ );
echo $user_login, ‘. Logout‘;
} else {
wp_login_form(array( ‘value_remember’ => 1, ‘redirect’ => $redirect));

if ( $show_reg && get_option(‘users_can_register’) ) {echo wp_register(”, ”);}
if ( $show_reg && $show_for) {echo ‘ | ‘;}
if ( $show_for) {echo ‘Lost Password‘;}

}

/* After widget (defined by themes). */
echo $after_widget;
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;

/* Strip tags (if needed) and update the widget settings. */
$instance[‘title’] = strip_tags( $new_instance[‘title’] );
$instance[‘errormsg’] = strip_tags( $new_instance[‘errormsg’] );
$instance[‘lostmsg’] = strip_tags( $new_instance[‘lostmsg’] );
$instance[‘show_for’] = $new_instance[‘show_for’];
$instance[‘show_reg’] = $new_instance[‘show_reg’];
return $instance;
}

function form( $instance ) {

/* Set up some default widget settings. */
$defaults = array( ‘title’ => ‘Log In’, ‘errormsg’ => ‘Error: Incorrect username/password’, ‘lostmsg’ => ‘Password reset sent.’, ‘show_for’ => false, ‘show_reg’ => false, ‘show_sex’ => true );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>

id=”get_field_id( ‘show_for’ ); ?>” name=”get_field_name( ‘show_for’ ); ?>” />

Widget Settings Widget Settings