How can i style "echo apply_filters"

With WordPress I have years of experience but with PHP I do not have much experience. I want the available product colors on the shop page and that's working! :)

But i want a text before the available colors and i want to style it. I tried a lot of things but my knowledge is limited.

Can someone help me? Thanks!

This is the code right now:

function custom_display_post_meta() {

    global $product;

    $attr = array('pa_kleur');

    foreach($attr as $attribute) {

        $values = wc_get_product_terms(
            $product -  id,
            $attribute,
            array(
                'fields' =
                'names'
            )
        );

        echo apply_filters(
            'woocommerce_attribute',
            wpautop( wptexturize( implode( ', ', $values ) ) ),
            $attribute,
            $values
        );

    }
}

Topic woocommerce-offtopic php filters Wordpress

Category Web


apply_filters just calls any methods hooked to the filter, in this case woocommerce_attribute, and returns the output. You could do a couple of things to style this, here's one way:

    $filtered = apply_filters(
        'woocommerce_attribute',
        wpautop( wptexturize( implode( ', ', $values ) ) ),
        $attribute,
        $values
    );

    echo '<span class="attribute-' . esc_attr( $attribute ) . ">' . $filtered . '</span>';

This will create something like:

<span class="attribute-color">RSV</span>

Now, you can style this in CSS with a rule such as:

span.attribute-color {
    font-weight: bold;
    /* etc */
}

This is assuming the attribute's name is "color", but whatever it is you should now be able to target that particular bit of text and apply custom CSS to it.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.