添加过滤器以更改功能的特定部分(add filter to change specific part of function)

编程入门 行业动态 更新时间:2024-10-22 04:46:30
添加过滤器以更改功能的特定部分(add filter to change specific part of function)

我正在尝试从wc_cart_functions.php文件中的woocommerce中的按钮中删除一个类。 wc_add_to_cart_message函数中有一个字符串,用于插入此字符串:

<a href="%s" class="button wc-forward">%s</a> %s

function wc_add_to_cart_message( $products, $show_qty = false ) { $titles = array(); $count = 0; if ( ! is_array( $products ) ) { $products = array( $products ); $show_qty = false; } if ( ! $show_qty ) { $products = array_fill_keys( array_keys( $products ), 1 ); } foreach ( $products as $product_id => $qty ) { $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) ); $count += $qty; } $titles = array_filter( $titles ); $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) ); // Output success messages if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) ); $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) ); } else { $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) ); } wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) ); }

我试图创建一个只替换该特定字符串的过滤器,因为它出现了两次,在两个实例中我都希望删除该类。 这似乎不起作用:

add_filter( 'wc_add_to_cart_message', 'add_to_cart_mod'); function add_to_cart_mod($message) { $message = str_replace ( '<a href="%s" class="button wc-forward">%s</a> %s' , '<a href="%s" class="button">%s</a> %s', $message ); return $message; }

使用此过滤器设置,我仍然看到具有相同未更改类的按钮。 有什么想法吗?

I am trying to remove a class from a button in woocommerce that is in the wc_cart_functions.php file. There is a string in the wc_add_to_cart_message function which inserts this string:

<a href="%s" class="button wc-forward">%s</a> %s

function wc_add_to_cart_message( $products, $show_qty = false ) { $titles = array(); $count = 0; if ( ! is_array( $products ) ) { $products = array( $products ); $show_qty = false; } if ( ! $show_qty ) { $products = array_fill_keys( array_keys( $products ), 1 ); } foreach ( $products as $product_id => $qty ) { $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) ); $count += $qty; } $titles = array_filter( $titles ); $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) ); // Output success messages if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) ); $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) ); } else { $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) ); } wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) ); }

I tried to create a filter which would just replace that specific string, since it appears twice and in both instances I want the class removed. This does not seem to work though:

add_filter( 'wc_add_to_cart_message', 'add_to_cart_mod'); function add_to_cart_mod($message) { $message = str_replace ( '<a href="%s" class="button wc-forward">%s</a> %s' , '<a href="%s" class="button">%s</a> %s', $message ); return $message; }

With this filter set as it is, I still see the button with the same unchanged class. Any thoughts?

最满意答案

你的过滤器期望找到%s ,但是之前对sprintf调用取代了:这些%s不再存在了。

您可以尝试使用正则表达式:

$message = preg_replace ('/(<a [^>]+ )class="button wc-forward"/', '$1class="button"', $message );

[^>]+部分表示:任何不包含> (标记结尾)的字符序列。

$1表示:括号之间匹配的内容。 它可能类似于<a href="http://example.com" 。

Your filter expects to find %s, but that was replaced by the previous call to sprintf: these %s are not there any more.

You could try with a regular expression:

$message = preg_replace ('/(<a [^>]+ )class="button wc-forward"/', '$1class="button"', $message );

The [^>]+ part means: any sequence of characters that does not contain a > (end of a tag).

$1 means: whatever was matched between the parentheses. It could be something like <a href="http://example.com".

更多推荐

本文发布于:2023-08-07 16:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   功能   add   filter   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!