I'm trying to add custom attributes to product label in WooCommerce cart Here's what I'm using
add_filter( 'woocommerce_cart_item_name', 'woo_api_cart_item_name', 10, 3 );
function woo_api_cart_item_name($item_name, $cart_item, $cart_item_key){
(... some code)
if($status == "NEW"){
if(isset($cart_item["clientDesignURL"]))
{
$item_name.= " <span style='color:red;' >✕</span> <a style='color:white;' href='".$cart_item["clientDesignURL"]."' class='clientDesignURLButton'> Customize</a>";
}
else
{
$item_name.= "<p onclick='myCustomFunctionB()' class='clientDesignURLError'>-</p>";
}
}
else{
$item_name .= "<span onclick='myCustomFunctionA()' style='color:green;' class='".$cart_item["clientDesignID"]."'>✓ <img src='".$decoded_response->thumbUrl."'/></span>";
}
return $item_name;
}
I'm trying to add button next to every item in cart which does something in ajax. Problem is that woocommerce / wordpress removes "onclick" attribute.
I've tried other tags but all of them are removed. Browser inspector Changing theme doesn't work. I've checked all plugins and it didn't work either. Trying other tags like
doesn't work (whole tag got removed)
CodePudding user response:
add_filter( 'wp_kses_allowed_html', 'prefix_filter_allowed_html', 10, 2 );
/**
* Add "onclick" to allowed KSES output.
*
* @param $allowed
* @param $context
* @return mixed
*/
function prefix_filter_allowed_html( $allowed, $context ) {
if ( 'post' === $context ) {
$allowed['p']['onclick'] = true;
}
return $allowed;
}
Add onclick attribute to allowed HTML for <p>
tag