Widgets, WPML and Timber

I’m a big fan of Timber, a WordPress plugin which renders Symfony’s templating language Twig into php code.

If you use Timber you can now render all your output in a Twig file, instead of having to echo the logic and all elements in a the (php) widget file.

Something like this:

  1. {{ args.before_widget }}
  2.  
  3.     <div>Some content here</div>
  4.     <div>Some more content here</div>
  5.  
  6. {{ args.after_widget }}

which makes for cleaner template files.

So far no issues.

WPML

If you have a multilingual site, that’s where you might run into some issues.

If you have made a simple widget, which doesn’t have any widget options like widget title or another setting, you might think you don’t need the form and update option which are defined in the default usage of the widget.

That’s where you’re wrong, and so was I. If you use WPML, a widget ‘needs’ a setting in which language(s) it needs to be shown. And to store that you need a form and an update function.

So regardless if you want to use any options in your widget, ALWAYS include the form and update methods. Even if they’re empty. See below.

  1. public function form( $instance ) {
  2.     // outputs the options form on admin
  3. }
  4.  
  5. public function update( $new_instance, $old_instance ) {
  6.     // processes widget options to be saved
  7. }

Then add the widget to your widget area. If you have WPML installed you’ll see an option to show the widget in a particular language or all. Save it for a particular language, then change it back to all languages and save it again.

Why these steps ? I have noticed and deduced (with WPML support) if you save it on the first save with all languages, it wouldn’t show. Read the solution from support here. I don’t know if it has been solved in the meanwhile, if not this is your solution.

TLDR

Always include a form and update method in your widget class and save it twice (with the second being “All languages”).