The WordPress Widgets API is how various widgets are created and sidebars registered.
When creating a new widget there are variables that can be added to any widget. Those get their value from the register_sidebars
arguments.
args (string/array) (optional)
Builds Sidebar based off of ‘name’ and
‘id’ values. Default: None
name
– Sidebar name.
id
– Sidebar id.
before_widget
– HTML to place before every widget.
after_widget
– HTML to place after every widget.
before_title
– HTML to place before every title.
after_title
– HTML to place after every title.
Example:
<?php
add_action( 'widgets_init', 'prefix_register_sidebars' );
function prefix_register_sidebars() {
$args = array(
'name' => 'My Sidebar',
'id' => 'my-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',,
'after_widget' => '</div><hr />',
'before_title' => '<h5 class="widgettitle">',
'after_title' => '</h5>'
);
register_sidebars( $args );
}
Example Widget:
class MY_Widget extends WP_Widget {
function my_widget( $args, $instance ) {
$widget_ops = array(
'description' => 'My Widget Description'
);
parent::WP_Widget(false, 'My Widget Name', $widget_ops );
}
function widget() { // This controls the display of the widget
$title="My Widget Title";
echo $before_widget; // Outputs the the 'before_widget' register_sidebars setting
echo $title; //Will be wrapped in the 'before_title' and 'after_title' settings
echo '<p>This is my widget output</p>';
echo $after_widget; //Outputs the 'after_widget' settings
}
}
add_action( 'widgets_init', 'prefix_register_widgets' );
function prefix_register_widgets() {
register_widget( 'my_widget' );
}