Filter function for label in WordPress

I am developing a WordPress site that uses WooCommerce. WooCommerce capitalizes every word in product attribute labels, which affects readability for long labels.

function wc_attribute_label( $name, $product = '' ) {
global $wpdb;
if ( taxonomy_is_product_attribute( $name ) ) {
    $name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) );
    $label = $wpdb->get_var( $wpdb->prepare( "SELECT attribute_label FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $name ) );
if ( ! $label ) {
    $label = ucfirst( $name );
    }
} elseif ( $product && ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ]['name'] ) ) {
    // Attempt to get label from product, as entered by the user
    $label = $attributes[ sanitize_title( $name ) ]['name'];
} else {
    // Just format as best as we can
    $label = ucwords( str_replace( '-', ' ', $name ) );
}

return apply_filters( 'woocommerce_attribute_label', $label, $name, $product ); }

I need to change “ucwords” to “ucfirst” in this snippet from the above code:

Read More
$label = ucwords( str_replace( '-', ' ', $name ) );

I want to create a filter for this in my functions.php file. It seems like it should be easy, but I’m having a lot of difficulty. Any help would be much appreciated. This is what I have:

add_filter( 'woocommerce_attribute_label' , 'custom_attribute_label' );
function custom_attribute_label( $label ) {
$label = ucfirst ( $label );
return ( $label ); }

Related posts

1 comment

  1. The problem is the syntax of the add_filter.

    Have rectified the code .Try this one now and let me know if that helps.

    add_filter( 'woocommerce_attribute_label' , 'custom_attribute_label',99,3 );
    
    function custom_attribute_label( $label, $name, $product ) {
      $label = ucfirst ( $label );
      return ( $label ); 
    }
    

Comments are closed.