In the Appearance -> Widgets menu there is a list of widgets that you can drag and drop to show in the sidebar.

Where is the HTML/PHP code for these custom widgets?

I’ve been on WordPress’s Function Reference but couldn’t find anything. Surely these widgets must be pulled from a HTML/PHP template.

The reason I want to know is the widget titles by default are <h3> tags and i want to change them to <h5> tags. Also i need to add some <hr />‘s and other things.

I’ve looked in the theme/includes/widgets.php file but found nothing.

I’m using a copy of Twenty Eleven to modify my theme by the way.

The code in theme/sidebar.php is for (!dynamic_sidebar()), however my sidebar is dynamic so this code is useless.

2 s
2

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' );
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *