dousi4472 2017-02-28 11:17
浏览 199
已采纳

用短代码更改WooCommerce缩略图

I'm building a small photography site and looking to protect the images the best I can.

(I know, I know - you can't protect the images once they're on the internet)

So - to do this, instead of using an annoying disable-right-click-using-javascript method, I'm loading the watermarked thumbnail images using base64 to obfuscate the filepath.

Code I'm using, if anyone's interested. Feel free to suggest improvements/criticise:

$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'shop_catalog' ); ?>
<img src="data:image/gif;base64,<?php echo base64_encode(file_get_contents($image[0])); ?>">

This works great on content-single-product.php, but does anyone know what code is loading the images in the [best_selling_products] and [recent_products] shortcodes? I'm in class-wc-shortcodes.php, but the query doesn't seem to be loading the images from here...

So why don't I just load small thumbnail images? Good question. Because we're planning to sell photos through the site, the high-res image has to be loaded as the featured image (to be sent though to the printing API) - and anyone with half a brain can look at the thumbnail url and get the full high-res image url.

Any suggestions on how to achieve this, or am I just best off scrapping the shortcodes and building my own query?

  • 写回答

1条回答 默认 最新

  • dt4233 2017-03-02 10:40
    关注

    Okay - so as requested, here's my solution:

    As @helgatheviking mentioned, the template which controls the product overview iswoocommerce/content-product.php - or as WooCommerce puts it:

    The template for displaying product content within loops

    There are a number of hooks here, including woocommerce_template_loop_product_thumbnail - the hook which controls the product thumbnails. So quite simply (in the end) all we need to do is:

    1) Unhook the thumbnails using:
    remove_action ('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);

    2) Add an action with our own custom thumbnail, or anything else:

    add_action('woocommerce_before_shop_loop_item_title', 'woocommerce_custom_template_loop_product_thumbnail', 10);
    
    function woocommerce_custom_template_loop_product_thumbnail() {
        $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'shop_catalog' );
        echo '<img src="data:image/gif;base64,' .base64_encode(file_get_contents($image[0])) . '">';
    }
    

    Thanks to @helgatheviking for pointing me in the right direction.

    More on WooCommerce Hooks at: https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教