Custom widget only works once, cannot place in more than one widget area

Question

I have created this simple custom widget. It works as expected but only on one widget area.

<?php 
// Creating a custom widget 
    class mycustomwidget extends WP_Widget {
    function __construct() {
    parent::__construct(
    'mycustomwidget', 
    __('Social Icons', 'mycustomwidget_domain'), 
    array( 'description' => __( 'Social icons', 'mycustomwidget_domain' ), ) 
    );
    }
// Creating widget front-end
    public function widget( $args, $instance ) {
    $mymeta = apply_filters( 'widget_title', $instance['mymeta'] );
    echo $args['before_widget'];
    if ( ! empty( $mymeta ) ) {
      echo '<a class="mymeta" href="' . $mymeta . '" target="_blank">mymeta</a>';
    }
    echo $args['after_widget'];
    }
// Widget Backend 
    public function form( $instance ) {
    if ( isset( $instance[ 'mymeta' ] ) ) {
    $mymeta = $instance[ 'mymeta' ];
    }
// Widget admin form
    ?>
    <p>
    <label for="<?php echo $this->get_field_id( 'mymeta' ); ?>"><?php _e( 'mymeta:' ); ?></label> 
    <input class="widefat" id="<?php echo $this->get_field_id( 'mymeta' ); ?>" name="<?php echo $this->get_field_name( 'mymeta' ); ?>" type="text" value="<?php echo esc_attr( $mymeta ); ?>" />
    </p>
    <?php } 
// Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['mymeta'] = ( ! empty( $new_instance['mymeta'] ) ) ? strip_tags( $new_instance['mymeta'] ) : '';
    return $instance;
    }
// Class mycustomwidget ends here
    } 
// Register and load the widget
    function mycustomwidget_widget() {
        register_widget( 'mycustomwidget' );
    }
    add_action( 'widgets_init', 'mycustomwidget_widget' );
0
wally9 4 months 0 Answers 11 views 0

Leave an answer