HTML Ad Widget

Home » Snippets » HTML Ad Widget
0

Created with:

Widgets Generator

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
				/**
 * wpturbo_HTML_AD_Widget Class
 * 
 * This class sets up a custom widget for the wpturbo plugin
 * 
 * @package WPTurbo
 */
class wpturbo_HTML_AD_Widget extends WP_Widget {

	/**
	 * Initialize the widget
	 *
	 * @return void
	 */
	function __construct() {
		parent::__construct(
			'wpturbo_HTML_AD_Widget',
			esc_html__( 'HTML Ad Widget', 'wpturbo' ),
			[ 'description' => esc_html__( 'Place Your Advert HTML Here.', 'wpturbo' ), ]
		);
	}

	/**
	 * Widget Fields array
	 * 
	 * @var array $widget_fields Array of fields.
	 */
	private $widget_fields = [
		[
			'label' => 'Ad HTML',
			'id' => 'wpturbo-Ad-HTML',
			'default' => '<div>HTML HERE</div>',
			'type' => 'textarea',
		],
	];

	/**
	 * This function generates the content that will be displayed, which is determined by the user's input.
	 *
	 * @param array $args Array of arguments.
	 * @param array $instance Array of instance.
	 * 
	 * @return void
	 */
	public function widget( array $args, array $instance ) : void {
		// Before and after widget  is set by the theme.
		echo $args['before_widget'];

		// Check if user has set a title for this widget
		if ( !empty( $instance['title'] ) ) {
			// Before and after widget title is set by the theme.
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'];
		}

		// Now, output the generated fields
		echo '<p>' . $instance['wpturbo-Ad-HTML'] . '</p>';

		echo $args['after_widget'];
	}


	/**
	 * Generates the widget fields.
	 *
	 * @param array $instance The array of options.
	 * 
	 * @return void
	 */
	public function field_generator( array $instance ) : void {
		$output = '';
		foreach ( $this->widget_fields as $widget_field ) {
			$default = '';
			if ( isset($widget_field['default']) ) {
				$default = $widget_field['default'];
			}

			$widget_value = ! empty( $instance[$widget_field['id']] ) ? $instance[$widget_field['id']] : esc_html__( $default, 'textdomain' );
			switch ( $widget_field['type'] ) {
				case 'textarea':
					$output .= '<p>';
					$output .= '<label for="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'">'.esc_attr( $widget_field['label'], 'domtest' ).':</label> ';
					$output .= '<textarea class="widefat" id="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'" name="'.esc_attr( $this->get_field_name( $widget_field['id'] ) ).'" rows="6" cols="6" value="'.esc_attr( $widget_value ).'">'.$widget_value.'</textarea>';
					$output .= '</p>';
					break;

				default:
					$output .= '<p>';
					$output .= '<label for="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'">'.esc_attr( $widget_field['label'], 'textdomain' ).':</label> ';
					$output .= '<input class="widefat" id="'.esc_attr( $this->get_field_id( $widget_field['id'] ) ).'" name="'.esc_attr( $this->get_field_name( $widget_field['id'] ) ).'" type="'.$widget_field['type'].'" value="'.esc_attr( $widget_value ).'">';
					$output .= '</p>';
			}
		}
		echo $output;
	}

	/**
	 * Outputs the options form on admin
	 *
	 * @param array $instance The array of options
	 * 
	 * @return void
	 */
	public function form( array $instance ) : void {
		$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( '', 'textdomain' );
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'textdomain' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
		</p>
		<?php
		$this->field_generator( $instance );
	}

	/**
	 * Processes widget options to be saved
	 *
	 * @param array $new_instance The new array of options
	 * @param array $old_instance The old array of options
	 * 
	 * @return array $instance The updated array of options
	 */
	public function update( array $new_instance, array $old_instance ): array {
		$instance = [];
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
		foreach ( $this->widget_fields as $widget_field ) {
			switch ( $widget_field['type'] ) {
				default:
					$instance[$widget_field['id']] = ( ! empty( $new_instance[$widget_field['id']] ) ) ? strip_tags( $new_instance[$widget_field['id']] ) : '';
			}
		}
		return $instance;
	}
}

/**
 * Registers wpturbo_HTML_AD_Widget widget
 *
 * @return void
 */
function register_wpturbo_HTML_AD_Widget_widget() : void {
	register_widget( 'wpturbo_HTML_AD_Widget' );
}
add_action( 'widgets_init', 'register_wpturbo_HTML_AD_Widget_widget' );

			

Register an account to save your snippets or go Pro to get more features.