Hot answers tagged

94

When I started this answer it should be just a small note. Well, I failed. Sorry! Stay with me, there is a goody hidden deep down … How WordPress widgets are stored The list of widget is stored in an option named 'sidebars_widgets'. A var_export() may give something like the following: array ( 'wp_inactive_widgets' => array ( ), 'top-widget' =>...


16

Widget options are stored in the options table under 'sidebars_widgets' as a serialized array. See this post for a longer explanation. Changing the field per PHPMyAdmin is not recommended, use PHP, get_option() and update_option() instead, WordPress will take care for correct serialization then.


15

Please refer to the get_sidebar() and dynamic_sidebar() Codex entries. The get_sidebar( $slug ) template tag includes the sidebar-$slug.php template-part file. The dynamic_sidebar( $slug ) template tag outputs the $slug dynamic sidebar, as defined by register_sidebar( array( 'id' => $slug ) ).


12

After some research and based on the answer from Eugene Manuilov I made a function that adds custom classes to widgets in a specific sidebar ('sidebar-bottom' in my case) based on the number of widgets set in that sidebar. This will suit perfectly in horizontal sidebars and themes based on twitter bootstrap that need spanX class to adjust the element's width....


12

I just found the answer and instead of putting this in the Title: <!--:en-->My English Title<!--:--><!--:fr-->My French Title<!--:--> We need to put this code: [:en]My English Title[:fr]My French Title and qTranslate does the rest :)


10

Its the camel-case in the widget ID. The following works: add_action( 'widgets_init', 'ditto_register_sidebars' ); function ditto_register_sidebars() { register_sidebar(array( 'name' => __( 'Right Hand Sidebar' ), 'id' => 'right-bar', 'description' => __( 'Widgets in this area will be shown on the right-hand side.' ) ...


9

You just need to use the alternate syntax for foreach. From the php manual: The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes: foreach (...


9

Widgets DO appear in the Customizer, but only if you are on the page that they are used on. For example, I have a sidebar for the Archive page. When I'm in the Customizer and on the homepage, that sidebar doesn't appear. But if I navigate to an archive page while in the Customizer, it then appears in the widget area.


7

On a parallel thread over on the WordPress hacks forum, someone suggested using in_the_loop() and that works some of the time, with some plugins that use either the_content and/or the_excerpt, but not all of the time with all the plugins I've been testing against. Likewise, I've now done further testing using is_main_query() and that works some of the time, ...


7

Defining new sidebar with in your functions.php <?php if ( function_exists('register_sidebar') ) { register_sidebar(array( 'before_widget' => '<li id="%1$s" class="widget %2$s">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' )); }?> Once ...


6

As of at least 3.6, you can use the following code to modify the query used: add_filter('widget_posts_args', 'widget_posts_args_add_custom_type'); function widget_posts_args_add_custom_type($params) { $params['post_type'] = array('post','custom_type'); return $params; } Just add the types you want in the array for post_type and they should appear. ...


6

Get sidebar is a really thin wrapper around locate_template, which just searches the current child theme and parent theme directory for the given sidebar. get_sidebar in wp-includes/general-template.php: $templates = array(); if ( isset($name) ) $templates[] = "sidebar-{$name}.php"; $templates[] = 'sidebar.php'; // Backward compat code will be ...


6

Your best option is to get a random post author which is a user ID so here is a function to do just that: function get_random_author_wpa91320(){ global $wpdb; $id = $wpdb->get_var(" SELECT post_author FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY RAND() LIMIT 1 ...


6

The cart widget isn't showing because it's configured to not show on the cart and checkout page. If you want to change that take a look at class-wc-widget-cart.php, there you find the following line: if ( is_cart() || is_checkout() ) return; Change it to: if ( is_cart() ) return; To show the widget on the checkout page. Note: This will be, if done ...


6

Update: 12/19/15 : Here's a plugin on Github that I developed (using the method in answer I provided below) adding support for changing all widget to bootstrap components/styling. Original Answer I understand not wanting to use javascript, but it seems like overkill in my opinion to completely create a new widget just for the list-group class to be added ...


6

The call actually comes from themes compat like you can read here in source. So I'd imagine that you simply copy pasted the complete contents and therefore just added the _deprecated_file() call as well. :D Lesson to learn from this: Never blindly copy paste stuff without understanding what it does and why it does it.


5

Explanation There's the global array $wp_filters. This array stores some sub arrays. One for each priority. Each sub array then contains an array for each callback added. And the keys for those sub-sub arrays are the attached functions. Example If you do the following, somewhere in your code (preferable after the init hook): echo '<pre>'; ...


5

get_stylesheet_directory_uri() returns a value, it doesn’t print anything. So you have to use: echo get_stylesheet_directory_uri(); get_template_part() is just a wrapper for locate_template(). But the latter has one advantage: It returns the path of the file it has found. Try the following: $path = locate_template( 'sidebar-front.php', TRUE ); echo $path;


5

You can do it using Widget Context plugin or Widget Logic plugin. They work fine. I prefer Widget Context because it has more flexibility.


5

If you were to add var_dump($params); to the top of your callback you'd notice that it contains references to both the sidebar name and the sidebar ID. You can use those to control when this runs, provided you know the name or ID. function widget_params( $params ) { if ('Main Sidebar' === $params[0]['name']) { // do something } return $params; } ...


5

Create a function that register sidebars, using register_sidebar, starting from an option: add_action('widgets_init', 'my_custom_sidebars'); function my_custom_sidebars() { $sidebars = get_option('my_theme_sidebars'); // get all the sidebars names if ( ! empty($sidebars) ) { // add a sidebar for every sidebar name foreach ( $sidebars as $...


5

You could add this function to your functions.php file. add_filter( 'sidebars_widgets', 'disable_all_widgets' ); function disable_all_widgets( $sidebars_widgets ) { $sidebars_widgets = array( false ); return $sidebars_widgets; } You could also use the Wordpress conditional tags to disable widgets only on certain pages. For example; this would only ...


5

You could dynamically register sidebars. When you register sidebars, you could query for all of your animal pages, loop over all pages, and register a unique sidebar for each page. Adding a page would automatically create a sidebar for it. However - that's a lot of sidebars. For something tightly coupled with the page content, I would rather handle it with ...


5

The issue is in your params array. There was a missing closing > in before_widget and before-title should be before_title. Try this: array( 'name' => 'Sidebar', 'id' => 'sidebar-1', 'class' => 'custom', 'description' => 'Standard Sidebar', 'before_widget' => '<aside id="%1$s" class="widget %2$s">', '...


4

You can check the 'id' variable that is passed in the $args variable in the widget() function, it will give you the id of the widgitized area it is currently in. ex: public function widget($args, $instance) { if( $args['id'] == 'sidebar-1' ) { //do sidebar stuff } else { //default behavior } }


4

Not a good thing if you have a lot of categories, so be careful! First, add the following function in functions.php: add_action( 'widgets_init', 'generate_widget_areas' ); function generate_widget_areas() { //Do not create for uncategorized category $terms = get_categories('exclude=1&hide_empty=0'); foreach ($terms as $term) { register_sidebar( ...


4

It sounds like you do want a custom post type, you just want to set the public argument to false. The register_post_type() has lots of arguments that will let you do everything you probably want. Along with public, you'll probably set has_archive to false. Setting show_ui to true will allow you to manage the posts in the backend. If you want to group the "...


4

I guess you could take a big step using wp_list_categories() with a walker to add a additional Unordered List to every item. The code: $cat_id = get_query_var( 'cat' ); $subcats = get_categories( 'child_of=' . $cat_id ); // child categories class Cat_Walker extends Walker_Category { function end_el( &$output, $page, $depth = 0, $args = array() ) { ...


4

Use Conditional Tags to show content only if a certain condition is met. In your case, you're probably looking to use is_front_page(). <aside> <ul> <?php if ( function_exists( 'dynamic_sidebar' ) ) { if ( is_front_page() ) { if ( ! dynamic_sidebar( 'frontpage-widget-area' ) ) { ...


4

As alluded to by @Michael in the comments, the CSS classes for widgets depend on the sidebar they're in more than the widget themselves. Those widget-specific CSS classes can be useful, but not when you're trying to style every widget. It sounds like you may be able to use a normal element selector that targets anything in your sidebar, though I'm unclear ...


Only top voted, non community-wiki answers of a minimum length are eligible