Home › Forums › Backend Issues (wp-admin) › How to register multiple widgets correctly?
Hey guys,
I use the code below to register custom widget, add ACF fields to it and display everything on front-end.
class Custom_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'topbar_widget', // Base ID
__('Topbar widget', 'domainname'), // Name
array( 'description' => __( 'Email, phone, FB link', 'domainname' ), ) // Args
);
}
/**
* 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 ) {
echo $args['before_widget'];
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
?>
<a class="top-email" href="mailto:[email protected]"><?php the_field('email_topbar', 'widget_' . $args['widget_id']); ?></a>
<a class="top-phone" href="callto://12345678901234567890"><?php the_field('phone_topbar', 'widget_' . $args['widget_id']); ?></a>
<a class="top-fb" href="<?php the_field('fb_topbar', 'widget_' . $args['widget_id']); ?>" target="_blank">Facebook</a>
<?php
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( isset($instance['title']) ) {
$title = $instance['title'];
}
else {
$title = __( 'New title', 'domainname' );
}
?>
<?php
}
/**
* 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 = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // class Custom_Widget
// register Custom_Widget widget
add_action( 'widgets_init', function(){
register_widget( 'Custom_Widget' );
});
This code works great, but I would like to register a few other widgets – the question is – do I repeat every line of above code for every single registered widget or can I add somehow the new custom widgets between the lines of above code without repeating a whole bunch of code?
Thanks.
I think you’ll need to change some items; I’ve marked them with “CHANGE_THIS”.
class CHANGE_THIS extends WP_Widget
function __construct() {
parent::__construct(
'CHANGE_THIS', // Base ID
__('CHANGE_THIS', 'domainname'), // Name
array( 'description' => __( 'Email, phone, FB link', 'domainname' ), ) // Args
);
}
add_action( 'widgets_init', function(){
register_widget( 'CHANGE_THIS' );
});
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Plugin boilerplates can do some of the heavy lifting during initial development. We look at four options to speed up your plugin creation. https://t.co/ZtMsdBxAHw
— Advanced Custom Fields (@wp_acf) June 5, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.