WordPress.org

Ready to get started?Download WordPress

Forums

Shortcode in a shortcode in a widget (6 posts)

  1. Christopher Martin
    Member
    Posted 1 year ago #

    I need to put a shortcode in a shortcode in a widget. To be exact I need this in a widget:

    [wpv-view name="Bookmaker - Payment Type List View" sapymttype ="[types field="deposit-withdrawal"][/types]"]

    However the shortcode inside the shortcode is not shown properly, and this is shown at the end where the shortcode is being incorrectly truncated: [/types]"]

    This works in the main content, but in the widget it doesn't.

    I added this to funtions.php to originally make the shortcode work in the widget:

    add_filter( 'widget_text', 'do_shortcode');

    Many thanks for any help...

  2. s_ha_dum
    Member
    Posted 1 year ago #

    What widget?

  3. Christopher Martin
    Member
    Posted 1 year ago #

    just a normal text widget...

  4. s_ha_dum
    Member
    Posted 1 year ago #

    You could try changing the priority on your add_filter, something like add_filter( 'widget_text', 'do_shortcode', 1);. Maybe that will work, but you may have a fundamental conflict with the widget code itself. If so, you will need to create your own widget.

  5. Christopher Martin
    Member
    Posted 1 year ago #

    Thanks... I tried that, then I realised that if I remove add_filter( 'widget_text', 'do_shortcode'); from functions file completely the shortcode still works exactly the same!

    Unfortunately I only know enough to be dangerous, so I am not sure where to look next to try and get this working!

  6. s_ha_dum
    Member
    Posted 1 year ago #

    Copy the text widget shortcode from wp-includes/default-widgets.php. It is a PHP class named 'WP_Widget_Text'. The whole declaration is 'class WP_Widget_Text extends WP_Widget'. Copy the whole thing to your theme's functions.php and give it a new name by changing the 'WP_Widget_Text' part only, so that you have, for example, 'class My_Custom_Text_Widget extends WP_Widget'. Now edit the 'widget' function inside that class. Just before $text = apply_filters... you want to put $text = do_shortcode($text); Now put register_widget('My_Custom_Text_Widget'); underneath the whole 'My_Custom_Text_Widget' class. That is, after the closing '}' of that that class.

Topic Closed

This topic has been closed to new replies.

About this Topic