• Resolved Arnopello

    (@arnopello)


    Hi

    I’m looking to include links in the custom products attributes on my website (in my case, links to PDF technical sheets). Problem is Woocommerce sanitizes HTML markup and displays it as text instead of interpretating it.

    I have searched the web extensively, tried every snippet of PHP code proposed, nothing worked.

    Only solution I found working was to include “html_entity_decode( … )” in the product_attributes.php Woocommerce file. It gives:

            <td class="woocommerce-product-attributes-item__value"><?php echo html_entity_decode( wp_kses_post( $product_attribute['value'] ) ); ?></td>

    However, after each update (and they come often), it resets even though the file has not been improved. So I have to make the change over and over. I’m looking for some solution where I don’t have to change that file every time I’m updating Woocommerce.

    Does anyone have a simple solution? Through functions.php or anything, but just telling Woocommerce “don’t sanitize links”?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi,

    To avoid modifying the WooCommerce core files and having to make the change after each update, you can use a custom plugin or your theme’s functions.php file to disable HTML sanitization specifically for product attributes. Here’s a step-by-step guide:

    Step 1: Create a Custom Plugin (Optional)

    You can create a custom plugin to add this functionality. If you prefer to add it directly to your theme’s functions.php file, you can skip this step.

    1. In your WordPress directory, navigate to /wp-content/plugins/.
    2. Create a new folder for your custom plugin. For example, name it custom-woocommerce-attributes.
    3. Inside this folder, create a PHP file, such as custom-woocommerce-attributes.php.

    Step 2: Add Code to Disable HTML Sanitization

    Edit the custom-woocommerce-attributes.php file (or your theme’s functions.php if you skipped creating a plugin) and add the following code:

    <?php
    /**
     * Plugin Name: Custom WooCommerce Attributes
     * Description: Disable HTML sanitization for WooCommerce product attributes.
     * Version: 1.0
     */
    
    function disable_attribute_sanitization($value, $attribute) {
        // Check if the attribute is 'pa_custom_attribute' (replace with your attribute name).
        if ($attribute == 'pa_custom_attribute') {
            return $value;
        }
        return wp_kses_post($value);
    }
    
    add_filter('woocommerce_variation_option_name', 'disable_attribute_sanitization', 10, 2);

    Make sure to replace 'pa_custom_attribute' with the name of your product attribute. You can add this code to your custom plugin or directly to your theme’s functions.php file.

    Step 3: Activate the Custom Plugin

    If you created a custom plugin in Step 1, activate it in your WordPress admin panel under “Plugins.”

    Step 4: Update Product Attributes

    With this code in place, WooCommerce should no longer sanitize HTML markup for the specified product attribute. You can now include links in your custom product attributes, and they should be displayed as expected. Be cautious when allowing HTML in attributes, as it can pose security risks if not properly validated.

    Hi there @arnopello ??

    Thank you for contacting Woo support!

    HTML in product attributes ( tag)

    If I get it, you would like to add HTML in the product tags at your site. Is this correct?

    The workaround provided in the thread linked here, needs the script from this documentation page, in order to work as expected.

    That code can be added via a plugin that allows custom functions to be added, such as the?Code snippets?plugin.

    I trust that points you in the right direction, but if you have more questions, let us know.

    We’re happy to help.

    Thread Starter Arnopello

    (@arnopello)

    Hi

    First thanks both for providing solutions I haven’t seen in my extensive search on the web, or better explained.

    I keep them close as they may be useful eventually. In the meantime, I have noticed the file which manages product attributes (namely product-attributes.php) has a big comment on the top saying “This template can be overridden by copying it to yourtheme/woocommerce/single-product/product-attributes.php”. So I assumed that this file does not change that much (it basically just displays all the attributes if I’m not mistaking) and just did that.

    I added my usual “html_entity_decode()” in the file, did a woocommerce update and it stayed the same. I would work this way unless I find some bugs appearing there.

    Hey there @arnopello,

    Thank you so much for adding your input! Some of our customers might indeed find this guide helpful!

    We appreciate you being an active part of the community ??

    Have a wonderful day!

    Thread Starter Arnopello

    (@arnopello)

    Update: actually my custom product-attributes.php file gets deleted when updating the theme (and another file I created to implement a custom shortcode). Functions.php seems to get reset too, meaning any function I would add there would need to be re-added after each update (relating to @owadud655 solution).

    I’ve read here and there that making a child theme would be the solution. I may do it eventually.

    In the meantime I’ll just reupload my files and modifications each time I update. Theme gets updated less than woocommerce, and for less critical stuff usually, so that would be less work.

    I see. Feel free to check out the Code Snippets plugin, @arnopello . It is an easy, clean, and simple way to run code snippets on your site. It removes the need to add custom snippets to your theme’s?functions.php?file.

    I hope that helps!

    Thread Starter Arnopello

    (@arnopello)

    That is a good idea! I’ll do that and will test next update.

    Thanks mate

    I’m glad we were able to help, @arnopello ! If you have a few minutes, we’d love if you could leave us a review: https://www.ads-software.com/support/plugin/woocommerce/reviews/

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘HTML in product attributes ( tag)’ is closed to new replies.