0
votes

I'm using the wordpress php widget but I am unable to process Wordpress shortcode - the php just renders the shortcode instead of processing it. HEre is what I did

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

    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

    <?php
    $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 ideas?

1

1 Answers

0
votes

The asin parameter value might be confusing the shortcode processor; try putting quotes around it.

echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default"] </div>');

Alternatively, does the shortcode accept each argument to that parameter as a separate parameter?

echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '" template="buynowamazon_widget" chan="default"] </div>');

Edit: Looking at the plugin's code, I don't think you need these lines:

add_filter('widget_execphp', 'shortcode_unautop');
add_filter('widget_execphp', 'do_shortcode');

They will try to process the shortcode, before the PHP is executed, and thus you won't get the shortcode correctly processed. Comment them out and see what the plugin does now.