Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 7 • Making the Most of WordPress Plugins 207


}

function smashing_widget_init() {
register_widget( 'SmashingHello' );
}

add_action( 'widgets_init', 'smashing_widget_init' ) );

Figure 7-9 depicts the widget.


Figure 7-9: The widget you just created, dropped in the sidebar widget area.


There you have it, a widget where you can change the title and output some text. This can
just as easily be something else, thanks to the fact that widget output is just PHP spitting
something out.


Another thing to remember is that not all widgets need to take options. If you just want to
drop an element in a widget area, with the ease it brings, then by all means just create the
widget and forget about the settings. It is really all about what you need in terms of
functionality.


DASHBOARD WIDGETS


You can create not only regular widgets, but also Dashboard widgets. This means that you can
add widgets to the admin area of WordPress, commonly referred to as the Dashboard. All those
boxes you see on the front page of the Dashboard are in fact widgets, and you can add your own.


To create a Dashboard widget, you need to create a plugin and hence a file in which the code
goes. The following one is a simple reminder to the users of a group blog to visit the internal
pages, so it only outputs some text and some links. First you need to create the function that
does this:


function dashboard_reminder() {
echo '

Free download pdf