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."
Related
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.
I am creating a wordpress theme from scratch.
I created my own page.php, header.php , footer.php, sinngle.php .
When i install any 3rd party plugin , it does not work but works on any third party themes.
Do i need to include any predefined wordpress API to make it work ?
Yes, there are several functions you need to add in your theme to make fullfledged WordPress theme.
First read this thoroughly. https://codex.wordpress.org/Theme_Development
Some important points:
In header.php, there should be wp_head() function call just before </head>
In footer.php, there should be wp_footer() function call just before </body>
You need to use WordPress loop properly. https://codex.wordpress.org/The_Loop
If you want to register Navigation, Sidebar, etc then it should be done in functions.php
Display your widget in your theme pages like this:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header_sidebar') ) :
endif; ?>
You of course can replace the dynamic_sidebar above with the actual tag name of the 3rd-party widget you wish to integrate.
If you make your own widgets, register them in your functions.php
http://wpgyan.com/how-to-create-a-widget-area-in-wordpress-theme/
Example:
<?php
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
register_sidebar( array(
'name' => 'Header Sidebar',
'id' => 'header_sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpgyan_widgets_init' );
?>
I'm new to Wordpress and just created my first template.
In the functions.php I have put the following code, that function what I understand should call the plugins from the plugins directory:
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Widgetized Area',
'id' => 'widgetized-area',
'description' => '',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
));
}
I do see all the plugins, and in the widget screen I also see the 'widgets' screen and it allows me to drag widgets into the working area.
The website itself displays the plugin's html but neither js nor css is working.
What am I missing?
Above code that you have added in your functions.php is to register sidebar not to call any plugin. It do not have any connection to plugin.
You can call sidebar you added in template as follow:
if ( is_active_sidebar( 'widgetized-area' ) ) {
dynamic_sidebar( 'widgetized-area' );
}
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.
I'm new to WordPress theming and have downloaded the Starker's WP theme from starkers
My question is, as this is an updated theme based on new twentyten for WordPress 3.0, do I need all the code in the functions.php file as it all seems to make reference to default twentyten theme?
I'm am doing a CMS and was wondering if there is any important code I need in this functions.php file?
Thanks.
It depends on what stuff you are going to incorporate in your theme. You don't need that all unless you want.
The minimum you need is for sidebar integration, here is an example:
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<div class="side">',
'after_widget' => '</div></div>',
'before_title' => '<div class="sidebar_title">',
'after_title' => '</div><div class="side_content">',
));
This is usually helpful when you want to assign what should come before or after the sidebar.
For example, I created a theme using Starker's theme and here is all what is present in my functions.php file:
<?php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<div class="side">',
'after_widget' => '</div></div>',
'before_title' => '<div class="sidebar_title">',
'after_title' => '</div><div class="side_content">',
));
// add thumbnail support to theme, options will be automatically visible in admin
if (function_exists('add_theme_support')) add_theme_support( 'post-thumbnails' );
?>