I want to prepend an SVG icon to the Add to Cart message in WooCommerce, but for some reason the SVG is not shown in the message.
I have the following code in myfunctions.php
file:
function update_add_to_cart_message($message, $product_id) {
$icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"></path><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"></path></svg>';
$message = $icon . $message;
return $message;
}
add_filter('wc_add_to_cart_message_html', 'update_add_to_cart_message', 10, 2);
If I replace the svg with something like$icon = '<span>Test</span>';
, it works fine.
Why isn't this working and what do I need to do to fix it? I could of course update the WooCommerce template file, but I prefer not to.
WooCommerce is usingwp_kses()
function withwp_kses_allowed_html('post')
context to filter notices https://docs.woocommerce.com/wc-apidocs/source-function-wc_add_notice.html#220.post
context doesn't have<svg>
and<path>
tags whitelisted, this is the reason why your svg code is getting stripped.
As a blunt workaround, you can hook to and add svg-related tags and attributes to whitelist for
post
context:
function wp_kses_allowed_html_63014342( $allowedposttags, $context ) {
if ( $context === 'post' ) {
$allowedposttags['svg'] = array(
'xmlns' => true,
'viewbox' => true,
);
$allowedposttags['path'] = array(
'd' => true,
'fill' => true,
);
}
return $allowedposttags;
}
add_filter( 'wp_kses_allowed_html', 'wp_kses_allowed_html_63014342', 10, 2 );
Note this would allow these tags for everything that is usingpost
context though.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.