carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

Can no longer set sidebar options on Sidebar field

Open smilingpeanut opened this issue 9 years ago • 6 comments

This field is missing the functions to set the options for new sidebars, ie. before_widget, after_widget, etc. At the moment, the only solution is to use the older, deprecated Choose_Sidebar field. Is this the intended behavior?

smilingpeanut avatar Mar 16 '17 15:03 smilingpeanut

Hi @smilingpeanut ,

The new field allows changing these options through a filter rather than a method:

$options = apply_filters( 'carbon_custom_sidebar_options', $options, $id );
register_sidebar( $options );

atanas-dev avatar Mar 16 '17 15:03 atanas-dev

Would you be able to provide an example with a bit more context? I'm trying to do the same thing and am not able to get it to apply any of the custom arguments I've set.

SmashBrando avatar May 12 '17 16:05 SmashBrando

This should do the job:

add_filter( 'carbon_custom_sidebar_options', 'my_custom_sidebar_options', 10, 2 );
function my_custom_sidebar_options( $sidebar_options, $sidebar_id ) {
	// see https://codex.wordpress.org/Function_Reference/register_sidebar for more info on the available options
	$sidebar_options = array(
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget'  => '</li>',
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => '</h2>',
	);
	return $sidebar_options;
}

atanas-dev avatar May 12 '17 20:05 atanas-dev

@atanas-angelov-dev This is what occurs when using that code verbatim:

Notice: register_sidebar was called incorrectly. No id was set in the arguments array for the "Sidebar 3" sidebar. Defaulting to "sidebar-3". Manually set the id to "sidebar-3" to silence this notice and keep existing sidebar content. Please see Debugging in WordPress for more information. (This message was added in version 4.2.0.) in [...]/wp/wp-includes/functions.php on line 4138

The options are not applied. Any thoughts?

smilingpeanut avatar May 15 '17 17:05 smilingpeanut

I'll update this ticket once we have had time to investigate.

atanas-dev avatar May 16 '17 06:05 atanas-dev

Fixed example:

add_filter( 'carbon_custom_sidebar_options', 'my_custom_sidebar_options', 10, 2 );
function my_custom_sidebar_options( $sidebar_options, $sidebar_id ) {
	// see https://codex.wordpress.org/Function_Reference/register_sidebar for more info on the available options
	$sidebar_options = array_merge( $sidebar_options, array(
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget'  => '</li>',
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => '</h2>',
	) );
	return $sidebar_options;
}

atanas-dev avatar May 16 '17 11:05 atanas-dev