Some backstory –
WordPress removed the ability to add shortcodes to links (and any tag attribute) in version 4.2.3 (I believe), for security reasons. So using it like that is no longer an option.
This is what you need to do (a two-part process):
- Add this filter/function to your theme’s
functions.php
file after the php opening tag <?php
and before the php closing tag ?>
(if there is any):
/**
* Bypass the current value of shortcode value wieh certain conditions apply.
*
* @param array $retarr Items from shortcode (already processed).
* @return array $retarr Corrected/adjusted Items
*/
function amazon_fix_my_own_shortcodes( $retarr ){
if( is_array( $retarr ) && !empty( $retarr ) ){
foreach( $retarr as $key => $val ){
if( isset( $retarr[ $key ][ 'link_clean' ] ) && isset( $retarr[ $key ][ 'price_clean' ] ) ){
$retarr[ $key ][ 'link_clean' ] = '<a href="' . $retarr[ $key ][ 'link_clean' ] . '"><button>' . $retarr[ $key ][ 'price_clean' ] . '</button></a>';
unset( $retarr[ $key ][ 'price_clean' ] );
}
}
}
return $retarr;
}
// below filer checks our shortcode array and adjusts it based on our new needs.
add_filter( 'amazon_product_in_a_post_plugin_elements_filter', 'amazon_fix_my_own_shortcodes' );
- Now adjust your shortcodes to be like this:
[amazon-element asin="B000I2PONS" fields="link_clean,price_clean" container=""]
Explanation – This adds a filter to WordPress that will alter the output of the plugin shortcode when both 'link_clean'
AND 'price_clean'
fields are present in the shortcode. Additionally, adding container=""
will clear the wrapper that is associated with the entire shortcode.
It will output what you need without needing to add all the HTML and shortcodes – the one new shortcode will output what you were adding with:
<a href="[amazon-element asin='B000I2PONS' fields='url_clean']">
<button>[amazon-element asin='B000I2PONS' fields='price_clean']</button>
</a>
.
Let me know if you have any issues.
Warm regards,
Don