Oct
11

Use a widget in a page or post with a shortcode

Shortcode any widget   , ,

Available  at wordpress now – This plugin allows you to use any widget as a shortcode in a page or post with any theme.  While many plugin authors do provide a widget and a shortcode, not all do.  With this plugin there is no need to use the hybrid theme or create a special page template just to use a widget in a page or post.

For example [do_widget "query posts"] will allow you to have a selection of posts listed in your page.    The plugin uses a dummy sidebar called “shortcode” which allows one to use the widgets user interface to specify the custom settings.

The shortcode sidebar is a named widget sidebar only used to set up the widget settings.

Warning: Themes with numbered (or totally unnamed sidebars) do not work well this. Somehow since the plugin is creating the shortcode sidebar rather than a theme (and plugins seem to run befor the theme code), wordpress allocates it the number one slot. Named sidebars are okay, but others relying on being no.1 or the one and only are pushed out.

Primarily I have in mind that one would want to list posts by excerpts  for a categeory or a tag group within a discussion of the subject.  Thus one could have multiple portfolio pages.  This is achieved using  Justin Tadlocks query posts widget with this do_widget shortcode.  One could also try using the template tags shortcode plugin also by Justin Tadlock to achieve similar functionality. The differences with this plugin are

  • any widget could be used (as opposed to template tags)
  • no special UI (User Interface) needed as each widget’s own UI is used to set any settings

I’d love to hear other ways that people might decide to use this plugin.

Download: amr_shortcode_any_widget

Demonstration of t wo widgets called with shortcodes from within page or post text

Demonstration of two widgets called with shortcodes from within page or post text

The Shortcode Sidebar - Add the widgets that you wish to use as shortcodes.

The Shortcode Sidebar - Add the widgets that you wish to use as shortcodes.

Set the widget's settings

Set the widget's settings

Edit thepage, inserting the do_widget shortcode

Edit thepage, inserting the do_widget shortcode

Share:
  • Digg
  • del.icio.us
  • Facebook
  • RSS
  • Technorati
  • Twitter

Related posts:

  1. Widget as shortcode – tips Want to keep the widget title to identify it from other instances of the same widget, but want to hide...
  2. Widgetized headers and footers? Dynamic (widgetized) Sidebars are great, but would you not like to have a dynamic header and footer in your theme...
  3. Subscribe2 Widget Form – an alternate The wordpress subscribe 2 plugin is great – however the form in the widget bothers me. I have raised this ...

15 Comments

1

Nice idea. What are the primary use cases you are targeting?

2

I’m using WP 2.8.5 and the Thematic Framework.
I get the following error trying to use a Query Posts Widget in my page with the following shortcode [do_widget "query posts"]:

Warning: array_intersect() [function.array-intersect]: Argument #2 is not an array in /*/*/*/*/wp-content/plugins/amr_shortcode_any_widget.php on line 57

line 57:
/* get the intersect of the 2 widget setups so we just get the widget we want */
$wid = array_intersect ($_wp_sidebars_widgets[$sidebarid],$widget_ids );
if ($debug) { echo ‘Chosen Widget ids in Chosen sidebar’;print_r($wid);}

I also wan’t to ask how can I use more than one instace of the same widget, since they have the same name.

Thank you,

3

Hi marcello,
a quick reply while on the run… Clearly looks like I need to error trap a bit more. Will also write up more.

Try entering Query Posts ie with the capitals. [do_widget "Query Posts"]

This will grab whether instances have been defined in the shortcode sidebar.

To access only ONE, view source to find the query post id and enter [do_widget id="query-posts-n"] .

Hope that makes sense for now.

4

Hey! thank you.
Works with [do_widget "Query Posts"] but it doesn’t work with [do_widget id="query-posts-n"], it doesn’t return any errors but renders no content. I inspected the widget and got it’s id that’s clearly visible in the widgets panel HTML code and when it renders correctly in the page.

any clue?

I’ll be using Display Widgets plugin to restrict appereance in the meantime.

Thank you.

5

Hi Marcelo, Sorry if in my rush I was not clear enough, Yes – Replace “n” with the number of the widget id.

IE: you need exactly the widget id that shows in the html source to isolate the exact instance if you are using a widget multiple times.

It seems from your comment that you had worked that out but did not realise that that was what I meant.

Do you have it working now?

6

Well – my requirement was to use query posts in a way that would work across themes – ie I did not want to have to write theme templates that did what query posts does.

QP did not have a shortcode and while I could modify it, I prefer to stay with the upgrade path.

Many author seems to deliver a widget without also doing a shortcode – it is quite easy to offer both callingthe same code.

…and so “do widget” was born. It can be used for any widget since it is essentially capturing the output of the widget function and returning that as the shortcode function result.

7

Hi.
I got it working perfectly with the [do_widget "Query Posts"] but not with [do_widget id="query-posts-n"] shortcode.

I replaced the n with the ID i got from inspecting the widget with firebug, in my case it’s query-posts-4, but it doesn’t work that way even if I have only this one widget. It returns empty content, but no errors.

Sorry if I wasn’t clear. But if it works for you, it’s sure something to do with my setup or a conflict with another plugin. (maybe).

cya

8

Hi Anna-Marie,

Nice plugin and it does what it “says on the tin”. My only issue with it is that it seems to interfere with the other Sidebars on my site. It’s as though, even though the sidebar isn’t displayed by the theme (Atahualpa342), the sidebar grabs the formatting done for the first sidebar on my site. So effectively side bar one loses formatting, side bar two uses formatting of side bar one, side bar three uses formatting of side bar two and so on (I have 5 sidebars).

I haven’t had time to properly investigate (mostly due to a 3 year old toddler running around wanting to play at pirates!), my initial thought is that the sidebar registered for the plugin might need to be in functions.php

Thanks again for the plugin. I have a feeling its gone to be exactly what I need to work with, as you suggested, the Query Posts plugin

Adrian

9

My primary use was to avoid creating a theme dependant template to list a subset of posts. So I wanted the widget “query post” functionality in a page – this would then work no matter what theme I switched to.

As far as other uses go…. anything is possible. Ideally of course every widget should also be available as a short code and this plugin would then be uneccessary.

10

Hi Adrian,
Yes I can see two year old pirates could be quite distracting – I have the ambitious young chef problem, but I think that is easier to deal with!

I think that there is a problem for the plugin with

  1. the way that wordpress does the sidebars and
  2. theme’s that use numbered sidebars rather than named ones (eg: the default theme)

Because the plugin is registering the “shortcode” sidebar, and plugins run before themes I think – it seems to grab the no. 1 slot. This seems to shift any theme numbered sidebar to 2nd place – this happens with the default theme. This is not a problem with themes that register and access their sidebars by name (eg: the ithemes series).

I did play around seeing if I could get the “shortcode” sidebar to register later, but no go.

Because of this I did spend some time looking at whether one could use the dummy “inactive widgets sidebar” and did hit some annoyances there and have not had a chance to get back to it.

This is sort of related to the Sidebars and Switching themes problem if one want to reuse a group of widgets – see http://wordpress.org/extend/ideas/topic.php?id=2550.

So to fix for you…. you could

  1. change your theme to use named sidebars – see the functions.php in your theme and the sidebar.php
  2. see if one of the other plugins will work better – there are a couple that say they create dummy sidebars that can then be accessed from a page. I suspect that they will have the same problem with your theme.
  3. Wait to see if I have time to work further on using the inactive widgets sidebar instead of a special “shortcode” one – that way one could configure the widget in the theme’s sidebar and then move it to inactive and access by id there
11

Thanks for the response Anne-Marie and yes, I agree it is undoubtedly an inherent problem with the theme and not the plugin, (or maybe not a problem, but perhaps a fundamental issue with sidebar management in WP, as it says also in the link you provided)

I did actually get the theme to work with the plugin by tweaking functions.php, but then, as we always do, went off at another tangent and ended up working on something else.

I’m quite enjoying learning about how WP fits together and looking at how plugins and themes are coded is a great help.

Thanks again and have a nice day

12

While I liked the feature that this plug-in gave at first, I have noticed in the second day using it that it doesn’t seem to play well with some other plug-ins. It sometimes switches the order of all of my widgets (on other pages and sidebars) around, and sometimes deletes the content within my other sidebars completely. I’m not sure if it’s just a specific plug-in that I have that it doesn’t work well with as I use several, but those who are planning to use this in conjunction with some of the more common plug-ins may want to beware. Back up the content & code within your sidebar widgets so that if it gets deleted you can at least replace it easily. I’m having to re-do a lot of things on our new site because I hadn’t anticipated this issue, but I at least wanted others to be aware.

If there is any fix I’d be interested to know what it is, because otherwise I’m left with creating the new page template, sidebar etc.

13

Dear IKS,
If you look back at the comments a couple before yours, eg: Adrians http://webdesign.anmari.com/shortcode-any-widget/#comment-3955
you will see that the problem occurs with themes that use numbered sidebars rather than named sidebars.

No information is deleted and the effect is NOT intentional. If you deactivate the plugin your theme should grab the no.1 spot again.

Some fix ideas are mentioned above. I had spend quite a bit if time looking at it, to try to get wordpress to add the shortcode sidebar later after the themes, however did not succeed and had to move on to real work. (I’ve never liked the way that wordpress does the sidebars and this is one of the reasons why – but perhaps the problem is the themes being a bit “casual” with their sidebars? or wordpress allowing a casualness?)

A nice big warning has now been added to the plugin home page.

14

Hi Anmari,

Unfortunately, even when I deactivated the plug-in nearly all of my content from the widgets in my other sidebars was gone (save for two widgets, I think). It’s rather unfortunate that the problem seems to be the theme, as I don’t intend to change it and I’m not sure how to go forward with the fix that involves editing functions.php and sidebar.php (I really wouldn’t know what to change). Oh well, the solution would have been perfect if it had worked.

15

I have a widget that is a daily quote, can I use this plug-in to post the quotes on a post page instead of the side bar and it include the previous quotes on the same page?

Leave a Comment