patric-boehner
8/28/2016 - 12:32 AM

Site Description Widget in Genesis

Site Description Widget in Genesis

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.


//* Header Widget
//**********************

add_action( 'widgets_init', 'pb_add_header_widget' );
	function pb_add_header_widget() {

	genesis_register_sidebar( array(
		'id'          => 'header-widget',
		'name'        => __( 'Header Widget', 'childtheme' ),
		'description' => __( 'This is the widget area for adding a cutom site header using the Site Description Widget.', 'childtheme' ),
	) );

}

add_action( 'genesis_site_description', 'pb_genesis_header_widget' );
function pb_genesis_header_widget() {

	genesis_widget_area ('header-widget',array(
       'before'		=> '<div class="header-description">',
       'after'		=> '</div><!-- End of Header Description -->',));

}
<?php

// Custom Site Description Widget
//**********************************************

/**
 * @package    CoreFunctionality
 * @since      2.0.0
 * @copyright  Copyright (c) 2016, Patrick Boehner
 * @license    GPL-2.0+
 */


//**********************************************
//* Security
//**********************************************

//* Blocking direct access to the plugin PHP file
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );


//**********************************************
// Setup Site Description Widget
//**********************************************

class PB_SiteDescription_Widget extends WP_Widget {
	/**
	 * Holds widget settings defaults, populated in constructor.
	 *
	 * @since 1.0.0
	 * @var array
	 */
	protected $defaults;
	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 */
	function __construct() {
		// widget defaults
		$this->defaults = array(
			'description'		=> '',
			'sub-description'	=> '',
		);

		// Widget Slug
		$widget_slug = 'site-description-widget';
		// widget basics
		$widget_ops = array(
			'classname'   => $widget_slug,
			'description' => 'Customize the site description that shows in the header of the website.'
		);
		// widget controls
		$control_ops = array(
			'id_base' => $widget_slug,
		);
		// load widget
		parent::__construct( $widget_slug, 'Site Description Widget', $widget_ops, $control_ops );
	}
	/**
	 * Outputs the HTML for this widget.
	 *
	 * @since 1.0.0
	 * @param array $args An array of standard parameters for widgets in this theme
	 * @param array $instance An array of settings for this widget instance
	 */
	function widget( $args, $instance ) {
		extract( $args );
		// Merge with defaults
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		echo $before_widget;

			if ( !empty( $instance['description'] ) ) {
			echo '<div class="site-description" itemprop="description">';

			// Description
			$description =  esc_html( $instance['description'] );
			echo '<p><span class="description">' .$description. '</span></p>';
			// Sub Description
			if ( !empty( $instance['sub-description'] ) ) {
				$sub_description =  esc_html( $instance['sub-description'] );
				echo '<p><span class="sub-description">' .$sub_description. '</span></p>';
			}

			echo '</div>';

			}

		echo $after_widget;
	}
	/**
	 * Deals with the settings when they are saved by the admin. Here is
	 * where any validation should be dealt with.
	 *
	 * @since 1.0.0
	 * @param array $new_instance An array of new settings as submitted by the admin
	 * @param array $old_instance An array of the previous settings
	 * @return array The validated and (if necessary) amended settings
	 */
	function update( $new_instance, $old_instance ) {
		$new_instance['description']  = esc_html( $new_instance['description'] );
		$new_instance['sub-description'] = esc_html( $new_instance['sub-description'] );
		return $new_instance;
	}
	/**
	 * Displays the form for this widget on the Widgets page of the WP Admin area.
	 *
	 * @since 1.0.0
	 * @param array $instance An array of the current settings for this widget
	 */
	function form( $instance ) {
		// Merge with defaults
		$instance = wp_parse_args( (array) $instance, $this->defaults );
		?>

		<p>
			<label for="<?php echo $this->get_field_id( 'description' ); ?>">Site Description:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'description' ); ?>" name="<?php echo $this->get_field_name( 'description' ); ?>" value="<?php echo esc_html( $instance['description'] ); ?>" class="widefat" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'sub-description' ); ?>">Sub Description:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'sub-description' ); ?>" name="<?php echo $this->get_field_name( 'sub-description' ); ?>" value="<?php echo esc_html( $instance['sub-description'] ); ?>" class="widefat" />
		</p>

		<?php
	}
}
add_action( 'widgets_init', create_function( '', "register_widget('PB_SiteDescription_Widget');" ) );