Quick question: shortcodes in beans widget areas


There's tons of plugins and functions.php snippets to enable WP to deal with using shortcodes in widgets, but I'm not sure that is necessary to reach for.

function beans_widget_area_shortcodes( $content ) {

    if ( is_array( $content ) )
        $content = build_query( $content );

    return beans_array_shortcodes( $string, $GLOBALS['_beans_widget_area'] );

}

This should accomplish the same, no? Though I'm not sure if I can use this as a general function, or whether I should extend it to include custom widget areas (for example: example_widget_area)?


Hey J.C.

Beans does not process shortcode in widgets and the function you mention is not related to it. That said Beans added a touch of magic and if you inspect a widget (text for example) you will see that the widget content is wrapped in an {$output-id}_output filter. Refer to this reply where I explain how the output with subfilters work (and link to various docs) which also apply to this situation.

For example you will see here that I have a text widget area with the output id open output: beans_widget_content[_sidebar_primary][_text][_text-2]. Here I can either add a filter to process shortcodes for all type of widgets, for all type of widgets but in the _sidebar_primary only, for only Text widgets, for only that particular _text-2 widget or a combination some of these conditions.

Let's say you want to process shortcode for the text widgets only, then you would add the following in your child theme functions.php:

add_filter( 'beans_widget_content[_text]_output', 'example_widget_process_shortcode' );

function example_widget_process_shortcode( $content ) {

  return do_shortcode( $content );

}

If you wanted to process to all type of widgets (be careful not to use unecessary memory usage), then you could do:

add_filter( 'beans_widget_content_output', 'example_widget_process_shortcode' );

function example_widget_process_shortcode( $content ) {

 return do_shortcode( $content );

}

I hope that make sense,


Hm, I'm guessing it would be best to be very selective with this - aka only doing xample_widget_process_shortcode for that particular widget where one might be used a (third party) shortcode.

Question: you mention memory usage. If I were to use a beans function to insert markup containing a third party shortcut on a page, would that be an issue?

Reason I ask is because I haven't found anything better to use than TOC+ for Jumplink enabled page toc's. With the type of material I'm dealing with, it makes sense to provide the reader with that functionality in Articles.

I'm not quire sure yet how to approach this (add_action / admin_init / custom fields versus add markup with two columns, one pulling in excerpt, the other containing the shortcode), but regardless of how, it would mean using the TOC+ shortcode.


Hey J.C.

Question: you mention memory usage. If I were to use a beans function to insert markup containing a third party shortcut on a page, would that be an issue?

Inserting content using Beans hooks is the correct way and performance wise it is 100%. When we speak memory ressource, we speak PHP in general (not Beans or WP). It sounds complicated but it is in fact super easy. To understand memory ressource you need to understand what your code do.

In the example above, if we apply the code to all the widgets, we must understand that for each widgets on the page it will run the code while it is only necessary to apply on one. This is theoretically how devs should think about using unnecessary memory.

Where it becomes a bit more complex is to find a good balance between performance and dynamic. In the widgets context, it isn't really a train smash to apply to all of them because the code ran on every widget is minimal so it will have a minimal effect which would be acceptable. We also don't want to have to manually add shortcode support for every widget we add (using the widget id in the hook) as it would become a maintenance pain. Here is when a dev have to make a choice, is the manual maintenance worth saving a tiny bit of memory.

In this case, we know that I would never have shortcode in a Category widget nor would we have one in the Calendar widget. However for the text widget, it is most likely that we would want to add another one at some point. So it is a wise choice to apply the shortcode to all the text widgets using beans_widget_content[_text]_output which will be dynamic and won't use much extra memory.

I hope this help you thinking code as engineer who care about practicality and efficiency without blindly ignoring performance.


Aha, actually, I think I see the logic. Memory is finite, so you have to be economical with it. Instead of each instance getting its own cut from the limited pie, it's only really necessary to cut once.

Would it be more economical to not make use of shortcodes if at all possible, but instead to integrate php for desired output within the beans function? I've always simply reached for the shortcode, so to speak, but since beans can output markup containing php ... might that be more economical?


Hey J.C.

It would use a little bit less memory but nothing major.

You have to think how you are going to use your website. Shortcode are good when you need to control something from the admin side (or your client) without opening your code. It is justified to do so and you have to keep praticality and workflow in mind too.

As I mentioned, it is really finding the balance and not also "hard code" everything and making your life complicated. Your application must talk to the backend admin to improve your workflow and that has a "memory cost" which is well justified.

Cheers,

Write a reply

Login or register to write a reply, it's free!