WordPress widget default content - wordpress

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.

Related

Register widget area above posts in WordPress Genesis child theme

I have a Genesis child theme that I'm working on (here's my test site) and I can't seem to figure out how to register a widget area above the posts. I see a lot of tutorials (like this one) that show how to add one that spans both columns (posts & sidebar), but I'd like to have one that is only above the posts, not the sidebar.
Looking at these hooks, it would be in the genesis_before_loop hook I believe. I tried tweaking the code in the tutorial I linked to but it didn't work out. Does anyone have the code or link to a better tutorial?
This is code I'm trying to use but it's not working:
/**
* Add widget above posts.
*/
//* Add widget above posts
add_action( 'genesis_loop’, ‘above_posts’ );
function above_posts() {
if (is_active_sidebar( ‘aboveposts’ )) {
genesis_widget_area( ‘aboveposts’, array(
'before' => '<div class=“aboveposts widget-area"><div class="wrap"',
'after' => '</div></div>'
) );
}
}
//* Register widget above posts areas
genesis_register_sidebar( array(
'id' => ‘aboveposts’,
'name' => __( ‘Above Posts’ ),
'description' => __( 'This is the above posts widget.’ ),
) );

Wordpress: functions.php Breaking Standard WP Functions

This issue is driving me crazy.
This morning, the Wordpress theme I'm creating included a very simple functions.php file, and my site worked properly. Today, I wrote a new function to iterate through categories and list certain posts, and that's the only change I made to functions.php.
After uploading the revised functions file to the server, I get a 500 error every time I access my site. I though, "OK, my new function broke something. I'll just remove it from functions.php and start over." However, after reverting to the original version of the file (the version that worked this morning), I'm still getting the 500 error.
If I remove functions.php from the server, my pages load (without their sidebars obviously, but they load). As soon as I upload the file again (same version that worked this morning), I get the 500 error.
I checked the error log, and I've found that, when I have functions.php on the server, Wordpress (or php more likely) can no longer find the get_template_directory_uri() function. I've done nothing to alter any of the standard Wordpress functions, and this only happens when I've got functions.php uploaded.
What might be causing functions.php to break the standard functions?
Here's my functions.php file:
<?php
/* *********************************************
* Create left-hand sidebar for single.php
*
* TODO: Finalize $sricks_single_left_sidebar.
* ********************************************/
$sricks_single_left_sidebar = array(
'name' => 'Left Sidebar',
'id' => 'lt-single',
'description' => 'This is the sidebar on the left side of the single.php template page.',
'before_widget' => '<!-- Left-Single Sidebar --><div class="lt-single">',
'after_widget' => '</div> <!-- End Left-Single Sidebar -->',
'before_title' => '<h1>',
'after_title' => '</h1>',
);
register_sidebar( $sricks_single_left_sidebar );
/* *********************************************
* Create header_search sidebar to be used on
* all pages
* ********************************************/
$sricks_header_search_sidebar = array(
'name' => 'Header Search',
'id' => 'header-search',
'description' => 'This is the search box in the page header.',
'before_widget' => '<!-- Header-Search Sidebar --><div class="header-search">',
'after_widget' => '</div> <!-- End Header-Search Sidebar -->',
'before_title' => '<h1>',
'after_title' => '</h1>',
);
register_sidebar( $sricks_header_search_sidebar );
?>
To find the cause of issues in WordPress when you get 500 level errors, a blank white screen, or any other strange or unexpected behavior, define the WP_DEBUG constant in wp-config.php.
Like this:
define( 'WP_DEBUG', true );
If you're developing or making changes to a site, it's usually a good idea to enable this feature until you're done editing. It can make it very easy to spot and correct mistakes that could otherwise vex for hours.
Thanks everyone, especially Spencer Cameron. It turns out I forgot the open and close parentheses in my new function's declaration. Wordpress reads functions.php before any of the built-in functions, so it just got stuck there.
When I submitted my code, I left out my new function because I had deleted everything from the body of the function; what could go wrong? Lesson learned...

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."

Remove empty titles in wordpress sidebar widgets

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.

Resources