Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save frankyonnetti/ae0b10a7cb0ccfb48345693395b3e06d to your computer and use it in GitHub Desktop.

Select an option

Save frankyonnetti/ae0b10a7cb0ccfb48345693395b3e06d to your computer and use it in GitHub Desktop.
WordPress - Custom widget plugin #wordpress #plugin #widget
<?php
/**
* Direction for starting a new widget plugin using this boilerplate.
*
* Replace the following:
* Plugin Name: "THEME Custom Widget Plugin"
* Description: "Custom widget for..."
* Machine Name: "THEME_Custom_Widget" with caps and underscores
* Machine Name: "theme_custom_widget" lower case and underscores
* Load: "theme_load_custom_widget" load widget lower case and underscores
* Machine Name: "theme-custom-widget" lower case with hyphens
* Class Name: "custom-widget"
*
* ACF directions below
*
*/
/**
* Plugin Name: THEME Custom Widget Plugin
* Description: Custom widget for...
* Version: 0.1.0
* Author: DesignHammer, LLC
* Author URI: https://designhammer.com
* License: GPL2
*/
// Creating the widget
class THEME_Custom_Widget extends WP_Widget {
/**
* Sets up a new Newsletter Signup widget instance.
*/
public function __construct() {
$widget_ops = array(
'classname' => 'theme_custom_widget',
'description' => __( 'Custom widget for...' ),
'customize_selective_refresh' => true,
);
parent::__construct( 'theme-custom-widget-select', __( 'THEME Custom Widget Plugin' ), $widget_ops );
// wp_enqueue_style(
// 'theme-custom-widget-styles',
// plugins_url( 'css/styles.css', __FILE__ )
// );
// wp_enqueue_script(
// 'theme-custom-widget-scripts',
// plugins_url( 'js/scripts.js', __FILE__ ),
// array(),
// false,
// true
// );
$this->alt_option_name = 'theme_custom_widget';
}
/**
* Outputs the content.
*
*/
public function widget( $args, $instance ) {
if ( !isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
// IF USING ACFs ADD THIS (part 1 of 2)
// widget ID with prefix for use in ACF API functions
$widget_id = 'widget_' . $args['widget_id'];
$title = ( !empty( $instance['title']) ) ? $instance['title'] : __( '' );
$body = ( !empty( $instance['body']) ) ? $instance['body'] : __( '' );
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); ?>
<?php echo $args['before_widget']; ?>
<div class="custom-widget-block">
<div class="custom-widget-wrap">
<?php if ( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<div class="custom-widget-body">
<p><?php echo $body; ?></p>
</div>
<?php
// IF USING ACFs ADD $widget_id (part 2 of 2)
the_field( 'field_name', $widget_id );
?>
</div>
</div>
<?php echo $args['after_widget']; ?>
<?php
}
/**
* Handles updating the settings for the current widget instance.
*
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['body'] = sanitize_text_field( $new_instance['body'] );
return $instance;
}
/**
* Outputs the settings form for the widget.
*
*/
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$body = isset( $instance['body'] ) ? esc_attr( $instance['body'] ) : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'body' ) ); ?>"><?php _e( 'Body:', 'text_domain' ); ?></label>
<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'body' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'body' ) ); ?>" cols="30" rows="10"><?php echo wp_kses_post( $body ); ?></textarea>
</p>
<?php
}
}
// Register and load the widget
function theme_load_custom_widget() {
register_widget( 'THEME_Custom_Widget' );
}
add_action( 'widgets_init', 'theme_load_custom_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment