I thought this would be the perfect time to introduce a new type of post I’ll be writing here at ptahd.com. Droppin’ knowledge on WordPress. Tips, tricks and lessons learned using WordPress. Everyone needs knowledge dropped on them about WordPress. Here, I’ll break it down so everybody’s in the loop!
So, how do you preset widgets to widget areas? In other words, how can you automatically assign widgets to a certain widget area (e.g. a sidebar).
I asked myself this seemingly impossible question on twitter a while ago. It was raised again by Justin so I thought I might take a stab at it. Turns out, it’s not that hard. Here’s what I came up with:
<?php
// Debug: Makes sure no widgets are registered already. If so, be gone.
//update_option( 'sidebars_widgets', NULL );
$current_theme = get_option( 'template' ); // variable stores the current theme
$target_theme = 'wpframework'; // variable stores the theme we want to target
$debug = false; // skip the ladie, da.
/**
* $preset_widgets:
* A two-dimensional array that stores all active sidebars and their active widgets
*
* Qiktip: Sidebars and widgets are identified by their unique ID
*/
$preset_widgets = array ( 'sidebar-1' => array( 'search', 'pages' ) );
// this checks to see if the user is trying to change themes, if so,
// compare that theme with our target theme.
if ( isset( $_GET['activated'] ) && $current_theme == $target_theme || $debug ) {
update_option( 'sidebars_widgets', $preset_widgets );
}
// Debug: What widgets/widget areas are active?
// Uncomment the next line and find out for yourself buddy ;P
//var_dump( get_option( 'sidebars_widgets' ) );
?>
The code is hopefully self explanatory. Copy and paste this into your theme’s functions.php or get the plugin version. From there, all you have to do is update two variables:
$target_theme= your targeted theme based on it’s file name.$preset_widgets= a two-dimensional array containing the sidebar id(s) and their respective widgets (based on their id(s))
That’s it. Just make sure you have those widget areas defined in your theme, or you won’t see it (of course). Finally, use this wisely. It’s bad practice taking any kind of action the user didn’t directly initate.
New series: Droppin’ knowledge on WordPress. http://tinyurl.com/ckkl8h
This comment was originally posted on Twitter
@ptahdunbar I dugg it! RT @ptahdunbar New series: Droppin’ knowledge on WordPress. http://tinyurl.com/ckkl8h (very cool hack / trick)
This comment was originally posted on Twitter
Thanks for this. It works great. I’m going to play around with it a bit.
I love this idea: “How to preset widgets to widget areas” http://bit.ly/10pyL3 .. will give a designer full control over his Child Theme
This comment was originally posted on Twitter
This is an interesting approach to be sure. I actually often tend toward the other side, going ahead and creating a custom template page for everything. I feel this makes the code more readable, but I might try something more like this next time I develop a theme. It’s a cool idea.
This comment was originally posted on http://www.wp-fun.co.uk/)“>WP FUN
I’ve been considering building this very theme. When I created the Query Posts widget, it was the first step toward this goal. Basically, it was an experiment to see if it was possible to really create a completely widgetized theme.
A very basic structure, maybe some extra code to handle the header stuff, code to handle pagination, a few extra widgets, and the addition of the WP hooks would be all that’s needed to make one of the most customizable WP themes around.
This comment was originally posted on http://www.wp-fun.co.uk/)“>WP FUN
Thank you for the post. The idea tickles the imagination. It’s do-able in own theme playground but could be stressful for sites to be turned over to clients. Imagine Tools > Upgrade to WP 2.8 or later WP 3.0 and widgets not compatible – egads, calls from irate clients Still, Thx again.
This comment was originally posted on http://www.wp-fun.co.uk/)“>WP FUN
I think the same, the point you described above will be enough to satisfy at least 90% of all theme users. The rest ones… still majority is the main and it’s better to come from their purposes then to try to created one for everybody but so complex that half of the theme users would give up using it at the first step(they will get lost in the themes options)
This comment was originally posted on http://www.wp-fun.co.uk/)“>WP FUN