[wp-trac] [WordPress Trac] #10976: Add before_content and after_content to widget options
WordPress Trac
wp-trac at lists.automattic.com
Tue Jan 3 16:57:56 UTC 2012
#10976: Add before_content and after_content to widget options
-------------------------------------------------+-------------------------
Reporter: newkind | Owner: azaozz
Type: enhancement | Status: reopened
Priority: normal | Milestone: Future
Component: Widgets | Release
Severity: normal | Version:
Keywords: 2nd-opinion has-patch needs-testing | Resolution:
-------------------------------------------------+-------------------------
Changes (by eddiemoya):
* keywords: 2nd-opinion has-patch, needs-testing => 2nd-opinion has-patch
needs-testing
Comment:
I am not sure if perhaps I am misunderstanding, but can not any theme add
custom variables when registering a sidebar?
I'll borrow an example from the codex page and modify it:
{{{
/* In theme functions.php */
register_sidebar(array(
'name' => sprintf(__('Sidebar %d'), $i ),
'id' => 'sidebar-$i',
'description' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' );
'custom_var' => 'something custom' //My custom variable
));
}}}
Then that variable will immediately be available for use within the
widget() method of WP_Widget. I'll once again borrow from a codex example
and modify it.
{{{
/* In your widget */
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title']
);
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title; ?>
Hello, World!
<?php echo $after_widget;
/** Using our custom variable **/
if ( $custom_var ) {
// Use this if the theme has given a value for our
custom variable
echo $custom_var;
} else {
// Some default value to use if the theme has not
supplied one
echo 'default stuff';
}
}
}}}
I kept it very simple in that example, and clearly there are better ways
to setup default values, especially if you have more of these custom
variables.
The only reason I could see an absolute imperative to bake in new specific
variables is because we want to encourage developers of publicly available
themes to define the new variables and style them accordingly, and that
might be a valid point. However if the primary concern is one of *how* to
create additional variables at all, and if you are the theme and widget
developer - doing this is already possible. If intended for public
consumption, a widget developer can always suggest theme developers use
the custom variables and set defaults for the many cases for which they
wont.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/10976#comment:17>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list