I am attempting to create some custom widgets.
I have created one, which only needed a different title field. However I am now stuck when trying to create a widget which allows me to have multiple fields. I need the following fields for the widget:
- Title
- Introduction text
- Main text
- Email address
I want the widget form to allow the user to update the fields above with text. I then want to output the text they have written in my sidebar widget.
I have the following code which is for the previous widget I made (with no extra fields other than title)
class registercv_widget extends WP_Widget {
function __construct() {
parent::__construct(
'registercv_widget',
__('Register CV Widget', 'registercv_widget_domain'),
array( 'description' => __( 'Provides a "Register CV" button which launches a pop-up', 'registercv_widget_domain' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
echo '<div class="widget-wrapper">';
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
echo __( '<div class="register-button"><span><div class="button white">Send your CV</div></span></div>', 'registercv_widget_domain' );
echo '</div>';
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Register as a candidate', 'registercv_widget_domain' );
}
// Widget admin form
?>
<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 esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
function registercv_load_widget() {
register_widget( 'registercv_widget' );
}
add_action( 'widgets_init', 'registercv_load_widget' );
This works fine but how can I add extra fields to this?
I have attempted copying all instances of $title
and making them again but with $text_one
but I do not understand how this field is being registered. Can anybody help?