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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看