Maybe this has happened to you: you’re plowing through a WordPress development project, perfecting custom fields and loops and all of a sudden you have an idea for a cool sidebar function.
Maybe it’s a featured posts widget or or integrating your favorite audio player API, whatever the purpose you have basically two options.
- Create a new plugin with separate styles and functions packaged all together,
- Build the widget into your theme functions.
The first option is the best practice for adding a custom widget to your theme. It allows for you to use that widget easily across themes and across your site and also makes it easy for the site admin to disable or enable at their leisure.
The second options is the quick and dirty way for you to add a widget to your theme. It literally takes seconds and you don’t have to worry about packaging styles and functions, you can just build them right into your theme files.
I can neither confirm nor deny rumors of a series of posts about the former, but this post is about the latter.
Edit Functions
Edit your functions.php file in your WordPress theme folder and add the “wp register sidebar widget” function, with your widget display name and its function callback.
[cc lang=”php”]
[/cc]
Write The Widget
In this example I’m basically writing a WP_query and some mark-up; not very complicated at all.
[cc lang=”php”]
Reader Interactions
There are 12 comments already... Come join us!
Trackbacks
[…] mentioned he liked the Rocket Plant featured posts widget on our page. Our featured posts widget is built into our theme functions which means when we upgrade to the new version of Standard Theme, I’m going to have to […]
[…] Posted by Tom McFarlin on Jan 26, 2011A few weeks ago, Brian Notess shared a great post on how to roll your own WordPress widget the easy way. In certain cases such as a time crunch or simple functionality, that works great.But if […]
Eric Dye says
Please, confirm or deny rumors of a series of posts about the former, I would love to read them.
Brian Notess says
I’ll have to let Tom answer that one.
Eric Dye says
Tom? Hello? (((echo)))
Tom McFarlin says
🙂
Daniel Merchen says
You mention that it’s not the best way to do widgets, however on the flip side, it’s rather effective. I like this idea because it allows you to basically role an entire CMS into your theme, and not require the user to install any outside dependencies.
Brian Notess says
You’re right Daniel, and it’s honestly the way I do it most often. Then if I’m feelig really ambitious I package it as a plugin.
I think it depends on what you are building.
Eric Dye says
If you build it into the theme, then you could have widgets only appear on certain pages … i.e. Categories widget listed on a blog page, but not on your about page, contact page, etc …
Correct?
Brian Notess says
I’ve never actually done this to date with widgets, but I believe that by adding conditional tags to your widget code (ie: is_page(‘home’) or is_single() etc…) you can make them only appear on specific pages.
http://codex.wordpress.org/Conditional_Tags
html5beta says
after add this widget,sidebar widget panel can not be dragable any more,and this widget can only use once.do anyone know why?thanks.
Chris Reed says
Just ran into this issue, I know this is an old post but, if it is okay to build shortcodes into your theme. Why would it break philosophy to build in widgets as well?