PHP Code Widget
Shortcode not working (2 posts)

  1. jncnath
    Posted 3 years ago #

    Hi Otto,

    The plugin works great but I'm having an issue with getting shortcode to be processed properly in the widget. Here is what I did:

    1. Added filters to my active theme function.php file

    // Allow shortcodes in text widgets
    		add_filter('widget_text', 'shortcode_unautop');
    		add_filter('widget_text', 'do_shortcode');
    		// Allow shortcodes in php code widget
    		add_filter('widget_execphp', 'shortcode_unautop');
    		add_filter('widget_execphp', 'do_shortcode');

    2. Added the following to the php widget

    $id = get_the_ID();
    $amazon_product_asin_value = get_post_meta($id, 'amazon_product_asin', true);
      echo do_shortcode('<div> [amazon asin=' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default] </div>');

    I also tried without do_shortcode and same result.

    #2 properly outputs the shortcode of [amazon asin=B008I20FT8&template=buynowamazon_widget&chan=default] which works fine if I just enter this in the standard text widget

    I'm using the Amazon Link plugin which generates the shortcodes

    Any idea?

    Thanks again.


  2. Samuel Wood (Otto)
    WordPress.org Tech Dude
    Plugin Author

    Posted 3 years ago #

    The plugin doesn't support shortcodes. I have no idea how to make them work in it.

    However, the widget_execphp filter is applied *before* the PHP is executed. So using PHP that creates a shortcode definitely won't work. Instead of creating a shortcode, just make your PHP create the HTML you want instead.

    I mean, what you're trying to do is kind of backwards. You're writing PHP to create a shortcode which will then be processed by other PHP to turn into HTML... Just skip the middleman.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic