7

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
  • Perhaps your looking for this: stackoverflow.com/a/2293494
    – Asko
    Jan 16, 2012 at 12:24
  • dynamic_sidebar() will do one of two things when called, it will either echo the sidebar, or return false. So if( !dynamic_sidebar() ) will either echo the sidebar, or do some sort of fallback.
    – mor7ifer
    Jan 16, 2012 at 13:46

2 Answers 2

10

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' );
}
1
  • where do i put this class in my child theme?
    – shorif2000
    Oct 20, 2014 at 15:05
0

it is in the functions.php

function twentyeleven_widgets_init() {

register_widget( 'Twenty_Eleven_Ephemera_Widget' );

register_sidebar( array(
    'name' => __( 'Main Sidebar', 'twentyeleven' ),
    'id' => 'sidebar-1',
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => "</aside>",
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.