patric-boehner
8/4/2016 - 9:14 AM

A simple call to action widget for wordpress with a title, two text areas, a button and area for shortcode.

A simple call to action widget for wordpress with a title, two text areas, a button and area for shortcode.

<?php

// Call To Action 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 CTA Widget
//**********************************************

class PB_CTA_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(
			'title' 					=> '',
			'subheading' 			=> '',
			'additional-heading' => '',
			'button' 				=> '',
			'link' 					=> '',
			'shortcode' 			=> '',
		);

		// Widget Slug
		$widget_slug = 'call-to-action-widget';
		// widget basics
		$widget_ops = array(
			'classname'   => $widget_slug,
			'description' => 'Display a call to action with either a linked button or a form anywhere in a widget area.'
		);
		// widget controls
		$control_ops = array(
			'id_base' => $widget_slug,
			//'width'   => '400',
		);
		// load widget
		parent::__construct( $widget_slug, 'Call-To-Action 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;

			// Title
			if ( !empty( $instance['title'] ) ) {
				echo $before_title . apply_filters( 'widget_title', $instance['title'] ) . $after_title;
			}
			// Subheading
			if ( !empty( $instance['subheading'] ) ) {
				$subheading =  esc_textarea( $instance['subheading'] );
				echo '<p><span class="cta-subheading">' .$subheading. '</span></p>';
			}
			// Additional Text Area
			if ( !empty( $instance['additional-heading'] ) ) {
				$additional_heading =  esc_textarea( $instance['additional-heading'] );
				echo '<p><span class="cta-add-heading">' .$additional_heading. '</span></p>';
			}
			// CTA Button Text & Link
			if ( empty( $instance['shortcode'] ) && !empty( $instance['button'] ) && !empty( $instance['link'] ) ) {
				$cta_button =  esc_textarea( $instance['button'] );
				$cta_link =  esc_url( $instance['link'] );
				echo '<span class="cta-button"><a href="' .$cta_link. '" target="_self" class="button">' .$cta_button. '</a></span>';
			}
			// Shortcode
			if ( !empty( $instance['shortcode'] ) ) {
				$shortcode =  do_shortcode( esc_textarea( $instance['shortcode'] ) );
				echo '<div class="cta-shortcode">'. $shortcode .'</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['title']			 = strip_tags( $new_instance['title'] );
		$new_instance['subheading']  = esc_html( $new_instance['subheading'] );
		$new_instance['additional-heading'] = esc_textarea( $new_instance['additional-heading'] );
		$new_instance['button'] = esc_textarea( $new_instance['button'] );
		$new_instance['link'] = esc_url( $new_instance['link'] );
		$new_instance['shortcode'] = esc_textarea( $new_instance['shortcode'] );
		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( 'title' ); ?>">Title:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'subheading' ); ?>">Subheading:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'subheading' ); ?>" name="<?php echo $this->get_field_name( 'subheading' ); ?>" value="<?php echo esc_textarea( $instance['subheading'] ); ?>" class="widefat" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'additional-heading' ); ?>">Additional Text:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'additional-heading' ); ?>" name="<?php echo $this->get_field_name( 'additional-heading' ); ?>" value="<?php echo esc_textarea( $instance['additional-heading'] ); ?>" class="widefat" />
			<small>Text is styled in italics.</small>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'button' ); ?>">Button Text:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'button' ); ?>" name="<?php echo $this->get_field_name( 'button' ); ?>" value="<?php echo esc_textarea( $instance['button'] ); ?>" class="widefat" />
			<small>Add your customized button text.</small>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'link' ); ?>">Button Link:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" value="<?php echo esc_url( $instance['link'] ); ?>" class="widefat" />
			<small>Add a link to an internal or external page.</small>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'shortcode' ); ?>">Shortcode:</label>
			<input type="text" id="<?php echo $this->get_field_id( 'shortcode' ); ?>" name="<?php echo $this->get_field_name( 'shortcode' ); ?>" value="<?php echo esc_textarea( $instance['shortcode'] ); ?>" class="widefat" />
			<small>For email and newsletter signups, add the shortcode for the form here. If using a form as your call to action, leave the button text and link fields empty.</small>
		</p>
		<?php
	}
}
add_action( 'widgets_init', create_function( '', "register_widget('PB_CTA_Widget');" ) );