Remove empty titles in wordpress sidebar widgets - wordpress

I have registered a wordpress sidebar like so:
register_sidebar( array(
'name' => __( 'First Sidebar', 'theme_name' ),
'id' => 'primary-widget-area',
'description' => __( 'The primary widget area', 'theme_name' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
The problem is that when the title is empty, the h3's still get rendered. Is there a way to remove these when the title is left blank?

This is just a small addition to Mark's answer. The default calendar widget uses if the title is empty, so it still displays an empty header. I worked round that by adding this to my theme's functions.php:
function foo_widget_title($title)
{
return $title == ' ' ? '' : $title;
}
add_filter('widget_title', foo_widget_title);
Changing 'foo' to something appropriate.

Printing the before_title and after_title is something that is done in the function widget( $args, $instance ) by the widget self. All of the default wordpress 3.1 widgets check if the title is empty before parsing before_title and after_title, but I guess you're using a custom widget from a theme or plugin, in that case you'll have to adjust the widget( $args, $instance ) code.

Wanted to thank Daniel James for his snippet - this is beautiful. I made a small change where I replaced with !no_display, then added !no_display to the title of my widgets in the front-end. This made it clear to my users that it was a hook to be referenced in a function (and not to be confused with a seemingly empty widget title).

Edit the template and check for the existence of a title. If no title is set do not print the h3.

Register two sidebars, identical but for the 'before_title' and 'after_title' values. Check for a title, and then call one or the other accordingly.

Related

WordPress widget default content

I am using a function to create my own widget in a custom theme, like this:
function my_sidebar_widget() {
register_sidebar( array(
'name' => 'My Sidebar',
'id' => 'my-sidebar-widget',
'before_widget' => '<div class="my-sidebar-widget">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'my_sidebar_widget' );
This works fine, mostly. The problem is that when my theme is activated on a fresh instance of WordPress, this widget automatically adds in a search bar, recent posts and recent comments.
My question: Is there a function that can remove the content of all widgets (or specific widgets)? Or at the very least, a function that lets you dynamically add default content into the widget (I'd just make it display an empty space)?
Just to be clear, I don't want to unregister the widget, I just don't want it to automatically add in a search bar, recent posts and recent comments.
I've seen some very convoluted solutions to this problem, but after looking in the database I realised there is a much easier way.
Upon theme activation, just update this option with nothing:
update_option( 'widget_block', '' );
Problem solved. Just bear in mind that this will blank the widgets every time the theme is activated.

wordpress contact widgets provide code to get each field individually

I am a wordpress developer, I need to display phone, fax, address and social menu in the site. sometime all info together and sometimes each field individually at different places. For example I need to show Phone number in header and phone, fax and address in footer and in contact us page. I need to add phone number once and call it individually at multiple places. Any help and suggestion will be appreciated.
Regards
Register it like widgets in your functions.php, for example:
function my_custom_widget() {
register_sidebar( array(
'name' => 'Contacts',
'id' => 'full_contacts',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'my_custom_widget' );
Then call it where ever you want in your templates with the widget ID:
dynamic_sidebar( 'full_contacts' );

Wordpress admin option for users to select custom Sidebar widgets per page

First of all, I have created sidebars on the widget panel:
add_action( 'widgets_init', 'my_register_sidebars' );
function my_register_sidebars() {
register_sidebar(
array(
'id' => 'sidebar1',
'name' => __( 'Sidebar1' ),
'description' => __( 'The Sidebar 1.' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
I have created a number of custom sidebar layouts. I want each of these layouts to be it's own widget, as an option like the default wordpress ones (e.g. archives, calendar, RSS, Search, Tag Cloud etc). So I can select which ones I want available for every site created.
Then depending on which sidebars I have made available I want the option for users (when creating / editing a page) to be able to choose from a dropdown sidebar options box, which layout they want on the relevant page - So this can then be called dynamically instead of having to call each layout specifically myself:
get_sidebar('$user-selected');
Apologies for waffling a bit, but any help is appreciated.
(p.s. I want to avoid using plugins)
Thanks, Matt.
Just use a custom field with a select box which displays all the available sidebars.
Then in your page template check for the custom field value and output accordingly.

unregister_sidebar not working, what am I doing wrong?

I'm creating a child theme for twentyeleven. I want to remove all the sidebars specified by default, and add one of my own. In the twentyeleven themes functions.php is this:
<?php
// etc etc etc
function twentyeleven_widgets_init() {
register_widget( 'Twenty_Eleven_Ephemera_Widget' );
register_sidebar( array(
'name' => __( 'Main Sidebar', 'twentyeleven' ),
'id' => 'sidebar-1',
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => "</aside>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// etc. 4 others registered too...
}
add_action( 'widgets_init', 'twentyeleven_widgets_init' );
// etc etc etc
?>
Now I realise that if I just comment out the add action, this will remove them all. I dont want to do this thought because when the parent theme is updated they will re-appear.
I've attempted to do this in the child-theme's functions.php without success:
<?php
// etc etc etc
function unregister_old_sidebars() {
unregister_sidebar('sidebar-1');
//and i've tried unregister_sidebar('Main Sidebar');
}
add_action( 'widgets_init', 'unregister_old_sidebars' );
// etc etc etc
?>
Codex says to use unregister_sidebar($id) where $id is "The ID of the sidebar when it was added". So... I guess it's the 'widgets_init' action that's wrong? Do the child theme functions not run AFTER the parent functions?
Help! :D
Ben
Ok I'm an idiot. Answered my own question by actually READING the docs (http://codex.wordpress.org/Function_Reference/unregister_sidebar):
"In the example, note that we assign a priority of 11 when registering the widgets_init hook. This is because a child theme's functions.php file is called before the parent theme's, which means that our call to unregister_sidebar() would accomplish nothing since the sidebar has not yet been registered."

Adding widget area to functions.php causes blank screen after posting anything in admin

I'm following multiple tutorials that all tell me to put this into my functions.php to add a widget area:
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '',
'after_widget' => '',
'before_title' => '<div class="title">',
'after_title' => '</div>',
));
?>
Once I upload that file and try to change anything, like post or edit a post I get a white screen directly after hitting publish or any other action button.
Is there something wrong with that code? Or am I missing something when it comes to adding widget areas to my template?
Wordpress doesn't like blank lines on the functions.php page..

Resources