Add Custom Widget in Wordpress

/** * Custom widget */ class Announcementtext_widget extends WP_Widget { public function __construct() { parent::__construct( 'announcementtext_widget', __( 'Announcement Text Widget', 'iplushtoy' ), array( 'classname' => 'announcementtext_widget', 'description' => __( 'Announcement Text Widget', 'iplushtoy' ) ) ); load_plugin_textdomain( 'iplushtoy', false, basename( dirname( __FILE__ ) ) . '/languages' ); } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { extract( $args ); $headingleft = $instance['headingleft']; $subheading = $instance['sub-heading']; $headingright = $instance['headingright']; $show_info = isset( $instance['show_info'] ) ? $instance['show_info'] : false; if($show_info == true): echo $before_widget; echo "<div class='heading-left'><h2>". $headingleft ."</<h2>"; echo "<h3>". $subheading ."</<h3></div>"; echo "<h2 class='heading-right'>". $headingright ."</<h2>"; echo $after_widget; endif; } /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['headingleft'] = $new_instance['headingleft']; $instance['sub-heading'] = $new_instance['sub-heading']; $instance['headingright'] = $new_instance['headingright']; $instance['show_info'] = $new_instance['show_info']; return $instance; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { $headingleft = $instance['headingleft']; $subheading = $instance['sub-heading']; $headingright = $instance['headingright']; $show_info = $instance['show_info'] ?> <p> <label for="<?php echo $this->get_field_id('headingleft'); ?>"><?php _e('Heading Left'); ?></label> <input type="text" class="widefat" id="<?php echo $this->get_field_id('headingleft'); ?>" name="<?php echo $this->get_field_name('headingleft'); ?>" value="<?php echo $headingleft; ?>"/> </p> <p> <label for="<?php echo $this->get_field_id('sub-heading'); ?>"><?php _e('Sub-heading'); ?></label> <input type="text" class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('sub-heading'); ?>" name="<?php echo $this->get_field_name('sub-heading'); ?>" value="<?php echo $subheading; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id('headingright'); ?>"><?php _e('Heading Right'); ?></label> <input type="text" class="widefat" id="<?php echo $this->get_field_id('headingright'); ?>" name="<?php echo $this->get_field_name('headingright'); ?>" value="<?php echo $headingright; ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'show_info' ); ?>"><?php _e('Enable this widget?'); ?></label> <input class="checkbox" type="checkbox" <?php checked( isset( $instance['show_info']), true ); ?> id="<?php echo $this->get_field_id( 'show_info' ); ?>" name="<?php echo $this->get_field_name( 'show_info' ); ?>" /> </p> <?php } } /* Register the widget */ add_action( 'widgets_init', function(){ register_widget( 'Announcementtext_widget' ); });
Adds a custom widget for wordpress. Paste in functions.php then modify as needed.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.