WordPress widgets in Twig

Okay, so I decided I had to be difficult and redo my site in Twig and Sass, which required two nifty plugins:

So, I am going to talk about the issues I encounter while trying to build new template with them. Otherwise it will be months before people hear from me again. Today’s topic is:

Enabling widgets in Twig

So I started with the starter template that comes with Timber, which, as it turns out, does not have any widgets enabled to show you how to add them.

This is one of those problems, where it seems straightforward, all the directions look just fine, but none of them work. Mostly because they are all missing one key piece of information or another.

The directions all look to be just fine for bespoke widgets, but I just want to use the dynamic WordPress widget containers so I could drag and drop things in Appearance.

So, what I ended up doing:

  • Define a widget in function.php. This is standard WordPress template building with label, ID, container, and title settings.
  • Add a call to the widget somewhere. This is where things fell down, because, for instance, examples would say just add $data['dynamic_widget'] = Timber::get_widgets('dynamic_widget'); at some undisclosed location. So first, let’s rewrite that rule a little so it makes sense:

    Which is to say, assign the ID of the widget container I just created to something I can call from the template. In my case:

    Which only leaves where to put it, and well, it turns out the starter template has a function called add_to_context. Any guesses on what it might be for? Right, adding things to the context array. In order to invoke our new widget container we need to define it as a context for the template.

    Add this statement to the add_to_context function.

And that is it really. You are done. You now have a happy little context that you can invoke from inside your moustaches anywhere you want:

3 thoughts on “WordPress widgets in Twig

  1. Hi, I discovered Timber fairly recently (we use Twig extensively and WordPress quite a bit so merging the two seems very natural) and I’ve been having a bit of trouble with this very issue.

    But I’ve got it to work by calling sidebar_id as a parameter of {{ dynamic_sidebar('sidebar_id') }}.

    Only trouble is that it doesn’t obey any configuration set for it in the dashboard UI; like, for example, the RSS widget won’t return the number of items that I’ve set it to. Any ideas?

    • And, to reply to myself, it now works.

      $context['dynamic_sidebar'] = Timber::get_widgets('');

      in the functions.php file seems to be all you need to register all widgets available, then you can call them in the template with

      {{ dynamic_sidebar(‘widget_id’) }}

      You don’t need, it seems, to specifically pass the ID of the widget when you initialise it in the functions.php file. Bit weird.

    • Ideas are still in short supply on this end. I am figuring things out as I go, when I have time. You can stuff many o’s into the spelling of noooob when talking about me and Twig.

Leave a Reply

Your email address will not be published. Required fields are marked *