php - WooCommerce add data- attribute to view cart button on success message -
i've managed change html markup of view cart button on success message add id="open_cart"
it, want add data- attribute such data-cart="open"
html output, id
returned.
any ideas on how add data-
attribute it?
function my_add_to_cart_message() { if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) : $message = sprintf( '%s<a id="open_cart" data-target="open-cart" href="%s" class="button">%s</a>', __( 'successfully added cart.' , 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'cart' ) ) ), __( 'view cart', 'woocommerce' ) ); return $message; } add_filter( 'wc_add_to_cart_message', 'my_add_to_cart_message' );
this function above returns:
<a id="open_cart" href="http://example.com/cart/" class="button wc-forward">ver carrinho</a>
the data-cart="open"
ignored. annoying.
here's quick explanation why happening.
take @ woocommerce success.php template responsible displaying success messages.
<?php foreach ( $messages $message ) : ?> <div class="woocommerce-message"><?php echo wp_kses_post( $message ); ?></div> <?php endforeach; ?>
the wp_kses_post() function sanitizes output of $message variable checking allowed tags , attributes.
here's solution:
add snippet functions.php
function my_filter_allowed_html($allowed, $context){ if (is_array($context)) { return $allowed; } if ($context === 'post') { $allowed['a']['data-cart'] = true; } return $allowed; } add_filter('wp_kses_allowed_html', 'my_filter_allowed_html', 10, 2);
you need hook wp_kses_allowed_html filter , add data attribute wp_kses_post() function doesn't filter out.
Comments
Post a Comment