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/

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?