I have added a function into my child theme to override the original function. This functions purpose is to display the products meta data in a list below the product.
The only part i can get past is the base64 image.
My current code is :
function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html = '';
$args = wp_parse_args(
$args,
array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
'label_before' => '<strong class="wc-item-meta-label">',
'label_after' => ':</strong> ',
)
);
foreach ( $item->get_all_formatted_meta_data() as $meta_id => $meta ) {
$value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
$key = wp_kses_post( $meta->display_key );
if($key === "Tie Preview"){
$b64 = explode(',', $value);
$image = base64_decode($b64[1]);
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . '<img src="data:image/png;base64,' . $image . '" />';
}else{
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . $value;
}
}
if ( $strings ) {
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}
$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
if ( $args['echo'] ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html;
} else {
return $html;
}
}
As you can see in the image below the image tag appears but the base64 image doesn't display.
Here is a screenshot of the data url loaded in a browser:
Has anyone else come up with this issue.
I have figured out the issue!!!!
instead of the <img>
i replaced it with and <a>
. I decided to do this as i could view the tool tip to see the out of the href attribute was.
as you can see somehow there is a <p>
being added. iv now taken the value and used substr()
to remove the <p>
at the front and end of the string.
My new code now looks like so:
function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html = '';
$args = wp_parse_args(
$args,
array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
'label_before' => '<strong class="wc-item-meta-label">',
'label_after' => ':</strong> ',
)
);
foreach ( $item->get_all_formatted_meta_data() as $meta_id => $meta ) {
$value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
$key = wp_kses_post( $meta->display_key );
if($key === "Tie Preview"){
$sub1 = substr($value, 3);
$sub2 = substr($sub1, 0, -4);
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . '<img src="' . $sub2 . '" />' . "<a href='" . $sub2 . "' >click here</a>";
}else{
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . $value;
}
}
if ( $strings ) {
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}
$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
if ( $args['echo'] ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html;
} else {
return $html;
}
}
` at the start? Sounds like the value you are reading from your database might have been treated as text content, and gotten the usual treatment of adding paragraphs? Maybe the method name in `$item->get_all_formatted_meta_data()` is already a clue here.
– CBroe Feb 06 '23 at 07:17