Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
762 views
in Q2A Core by

I have a 'social share' widget (using addthis), and I'd like to add it below the question. There doesn't seem to be a way to do this, so I am looking at adding the widget's content to my theme in the relevant place.

It would be great not to have duplication here. Is it possible to either

  1. declare a new widget location in a theme and then invoke it?
  2. or invoke a named widget directly?

Looking at qa-theme-base I think I should be able to do something like this for option 2:

my_social_share_widget_class->output_widget("main", "my-below-question", $this, $this->template, $this->request, $this->content);

and then check for the 'my-below-question' in my own widget.

Is this 'safe' in terms of any future plans for widgets?

 

 

2 Answers

0 votes
by

The right thing to use are Layers. You can override the main functions of the qa-theme-base, and keep compatibility. Also, with layers you don't need widgets.

You can take a look here:

http://www.question2answer.org/layers.php

by
I'm not convinced.

The widget system works well, from what I can see, and the fact that it's my widget in this case is a bit of an aside.

it would be useful to be able to define new widget locations to use other third-party widgets in those locations as they become available; this would also make it easier to produce themes for distribution with extra widget slots in them.
by
But layers are the right thing to do. Widgets are meant to be placed on specific places, and layers are meant to add HTML on any part of the page.
by
But (part of) my question is exactly this! Is it possible to declare those additional places from an advanced theme so general purpose widgets can be used in those new locations? You can see the value for theme developers here, surely?

The other part (calling a pre-existing widget) is merely a convenience to stop duplication of code already used in a widget. If I am implementing widgets for my clients, and themes for my clients, it would make sense to at least
be able to invoke pre-existing widget code to reduce code duplication, if it's not possible to create new widget locations for them for use from the admin screens.
0 votes
by

To answer my own question, it looks like declaring new widget locations from the theme would not be possible as it stands, because the theme class is loaded after qa_content_prepare() in qa-page.php. Also, the widget locations are (at least in the developer preview) declared twice - once in qa_content_prepare() and once in the function qa_admin_place_options() in qa-app-admin.php.

While obviously this is a developer preview, this looks like a missed opportunity to me. I reckon custom themes will often have a use for extra widget locations (equivalent to the way wordpress does things.)

@gidgreen, is it possible that the initialisation of the theme could be split so that a declaration like this (of an additional widget location) could be made in the theme to be available before the content is prepared?

Alternatively, could it be possible for theme developers to declare additional widget locations from some form of plugin that could be distributed along with it? I think this would improve the modularisation of themes and widgets.

...