/***Widget for Welcome Box *********/ class Welcome extends WP_Widget {
/** * Sets up the widgets name etc */ public function __construct() { // widget actual processes parent::__construct( 'welcome_user', // Base ID __('Welcome User', 'text_domain'), // Name array( 'description' => __( 'Shows Welcome Message if User is Logged In', 'text_domain' ), ) // Args ); }
/** * Outputs the content of the widget * * @param array $args * @param array $instance */ public function widget( $args, $instance ) { // outputs the content of the widget if(is_user_logged_in()) { $objCurrentUser = wp_get_current_user(); $strNiceName = $objCurrentUser->data->user_nicename; ?> <div class="welcome_text"> <?php echo __('Welcome '.$strNiceName); ?>, <a href="<?php echo wp_logout_url(get_option('siteurl')); ?>"><?php echo __('Log Out'); ?></a> </div> <?php } }
/** * Ouputs the options form on admin * * @param array $instance The widget options */ public function form( $instance ) { // outputs the options form on admin }
/** * Processing widget options on save * * @param array $new_instance The new options * @param array $old_instance The previous options */ public function update( $new_instance, $old_instance ) { // processes widget options to be saved } } add_action('widgets_init', 'fnWelcome'); function fnWelcome(){ register_widget( 'Welcome' ); } /********** END **********/
Another Way to Create Widget
register_sidebar_widget(<Widget Title>, <your Custom function name>);