Skip to content

Instantly share code, notes, and snippets.

@davidperezgar
Last active June 24, 2016 15:02
Show Gist options
  • Save davidperezgar/2dffe82be2f50c49e79767877f12ed79 to your computer and use it in GitHub Desktop.
Save davidperezgar/2dffe82be2f50c49e79767877f12ed79 to your computer and use it in GitHub Desktop.
WordPress Widget with field title
<?php
/*
Widget Name:
Description:
Author: davidperez
Author URI: https://www.closemarketing.es
*/
function widget_name() {
register_widget( 'widget_name' );
}
add_action( 'widgets_init', 'widget_name' );
class widget_name extends WP_Widget {
// CONSTRUCT WIDGET
function widget_name() {
$widget_ops = array(
'classname' => 'widget_name',
'description' => '',
'panels_icon' => 'dashicons dashicons-layout',
);
parent::__construct( 'widget_name', __( '', 'cmk' ), $widget_ops );
}
// CREATE WIDGET FORM (WORDPRESS DASHBOARD)
function form($instance) {
if ( isset( $instance[ 'widget_title' ] ) ) {
$widget_title = $instance[ 'widget_title' ];
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'widget_title' ); ?>"><?php _e( 'Título del Widget', 'tt' ); ?></label>
<input name="<?php echo $this->get_field_name( 'widget_title' ); ?>" type="text" value="<?php echo esc_attr( $widget_title );?>" class="widefat"/>
</p>
<?php
}
// UPDATE WIDGET
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['widget_title'] = ( ! empty( $new_instance['widget_title'] ) ) ? strip_tags( $new_instance['widget_title'] ) : '';
return $instance;
}
// DISPLAY WIDGET ON FRONT END
function widget( $args, $instance ) {
extract( $args );
// Widget starts to print information
$before_widget = str_replace('" id=', ' widget_fullwidth" id=', $before_widget);
echo $before_widget;
if (isset($instance[ 'widget_title' ]))
$widget_title = apply_filters( 'widget_title', $instance[ 'widget_title' ] ); else $widget_title = "";
?>
<?php
// Widget ends printing information
echo $after_widget;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment