Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got a routine (see below) that creates and inserts several widgets into the active theme's sidebars.

However, I need more control over either the order/index the are assigned (ie, one of my widgets need to be at the top of any other widgets in the sidebar) OR, I need to be able to specify a custom css name for the widget.

I've inserted a comment to catch the widget titled "my important widget" and apply the index or custom css I'll need to assign to that widget.

Are either of these possible?

$file_name_widget = $files_widgets->getPathname();
$widget_text = file_get_contents($file_name_widget);
$widget_text_label = 'text';
$widget_ops = get_option('widget_text', array());
$widget_id = count($widget_ops) + 1;
if($file_name_widget == 'my important widget') {
    //place code here to make this widget move to the top of any others in the sidebar its assigned to
    }
$widget_ops[$widget_id] = array('title' => $files_widgets->getBasename('.txt'),'text' => $widget_text,);
update_option('widget_text', $widget_ops);                  
$sidebar_id = basename($files_widgets->getPath());
$sidebars_widgets = get_option('sidebars_widgets', array());
$sidebars_widgets[$sidebar_id][] = $widget_text_label."-".$widget_id;
update_option('sidebars_widgets', $sideb
share|improve this question

3 Answers 3

up vote 0 down vote accepted

If you want to change the orders, you can use the filter sidebars_widgets, the argument to which is an array of all the widget ids and their locations. You can change the id positions in that array as per your need.

share|improve this answer

You are already specifying an ID.. check it in your output.. you can use it with css like this:

#idname { <-- this is how we target an ID
background: #f3f3f3;
some othere CSS...
}

Check the output / the page where it apears on your site for that widget id and use it.. you can also paste the id here and i would demonstrate css usage.

share|improve this answer

Have a look at the global variable global $wp_sidebars_widgets The indexes of this array are the positions that the widgets will display in.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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