“Woocommerce WP插件不允许使用外部URL设置产品图像。”

huangapple go评论136阅读模式
英文:

Woocommerce WP plugin does not allow to set product image using external url

问题

我正在创建一个网站,该网站使用不同的联盟营销计划,这些计划将被列为产品,并使用WooCommerce进行管理。我遇到的问题是,我只提供了用作联盟计划横幅的URL/HTML代码,而WooCommerce只允许我通过上传文件到媒体库来设置产品图像。您是否知道任何插件或创建自定义插件的方法,可以让我使用外部URL来设置图像?

非常感谢提前的帮助!

我尝试使用以下代码作为自定义插件,该插件将显示一个媒体框以输入URL,但在发布产品时无法保存它:

<?php
/*
Plugin Name: 从URL设置产品图像
Plugin URI: 
Description: 从外部URL设置产品图像以促进联盟网络
Version: 1.0
Author: 
Author URI: 
License: GPL2
*/

add_action( 'add_meta_boxes', 'product_image_url_meta_box' );

function product_image_url_meta_box() {
    add_meta_box( 
        'product_image_url',
        __( '产品图像URL' ),
        'product_image_url_meta_box_callback', 
        'product',
        'side',
        'high'
    );
}

function product_image_url_meta_box_callback( $post ) {
    $product_image_url = get_post_meta( $post->ID, '_product_image_url', true );
    echo '<input type="text" name="_product_image_url" value="' . esc_attr( $product_image_url ) . '" />';
}

add_action( 'woocommerce_process_product_meta', 'product_image_url_save' );

function product_image_url_save( $post_id ) { 
    $product_image_url = $_POST['_product_image_url'];
    
    if ( ! empty( $product_image_url ) ) {
        update_post_meta( $post_id, '_product_image_url', sanitize_text_field( $product_image_url ) );
    }
}

add_filter( 'woocommerce_product_get_image_id', 'product_image_url_image_id', 10, 2 );

function product_image_url_image_id( $image_id, $product_id  ) {
    $product_image_url = get_post_meta( $product_id, '_product_image_url', true );
    
    if ( ! empty( $product_image_url ) ) {
        $upload = get_post_meta( $product_id, '_product_image_url', true );
        $image_id = $upload;
    }
    
    return $image_id; 
}

尝试添加自定义插件,尝试了许多图像上传插件,但即使使用相对插件修改链接,也无法绕过广告拦截器浏览器扩展。

英文:

I am creating a website that uses different affilite marketing programs that will be listed as products using woocommerce. The issue I am having is that I am only provided with url /html code as images to be used as banners for the affiliate programs and woocommerce only let me set product images by uploading files on media library. Do you know any plugin or a way to create a custom plugin that will let me set the image by using an external url?

Thanks a lot in advance!

I was trying to use this code as custom plugin that would display a media box to enter the url but it won't save it when publishing product:

&lt;?php
/*
Plugin Name: Set Product Images from URL 
Plugin URI: 
Description: Set product images from an external URL to promote affiliate network 
Version: 1.0
Author: 
Author URI: 
License: GPL2
*/

add_action( &#39;add_meta_boxes&#39;, &#39;product_image_url_meta_box&#39; );

function product_image_url_meta_box() {
    add_meta_box( 
        &#39;product_image_url&#39;,
        __( &#39;Product Image URL&#39; ),
        &#39;product_image_url_meta_box_callback&#39;, 
        &#39;product&#39;,
        &#39;side&#39;,
        &#39;high&#39;
    );
}

function product_image_url_meta_box_callback( $post ) {
    $product_image_url = get_post_meta( $post-&gt;ID, &#39;_product_image_url&#39;, true );
    echo &#39;&lt;input type=&quot;text&quot; name=&quot;_product_image_url&quot; value=&quot;&#39; . esc_attr( $product_image_url ) . &#39;&quot; /&gt;&#39;;
}

add_action( &#39;woocommerce_process_product_meta&#39;, &#39;product_image_url_save&#39; );

function product_image_url_save( $post_id ) { 
    $product_image_url = $_POST[&#39;_product_image_url&#39;];
    
    if ( ! empty( $product_image_url ) ) {
        update_post_meta( $post_id, &#39;_product_image_url&#39;, sanitize_text_field( $product_image_url ) );
    }
}

add_filter( &#39;woocommerce_product_get_image_id&#39;, &#39;product_image_url_image_id&#39;, 10, 2 );

function product_image_url_image_id( $image_id, $product_id  ) {
    $product_image_url = get_post_meta( $product_id, &#39;_product_image_url&#39;, true );
    
    if ( ! empty( $product_image_url ) ) {
        $upload = get_post_meta( $product_id, &#39;_product_image_url&#39;, true );
        $image_id = $upload;
    }
    
    return $image_id; 
}

tried to add custom plugin, tried many image uploaders plugins that won't bypass ad blockers browser extensions even when modifying links with relative plugins.

答案1

得分: 1

您可以使用media_sideload_image()函数来从URL下载图像并保存到媒体库中。此更新后的代码将允许您通过输入外部URL来设置产品图像。

/*
Plugin Name: Set Product Images from URL
Plugin URI:
Description: Set product images from an external URL to promote affiliate network
Version: 1.0
Author:
Author URI:
License: GPL2
*/

add_action('add_meta_boxes', 'product_image_url_meta_box');

function product_image_url_meta_box() {
    add_meta_box(
        'product_image_url',
        __('Product Image URL'),
        'product_image_url_meta_box_callback',
        'product',
        'side',
        'high'
    );
}

function product_image_url_meta_box_callback($post) {
    $product_image_url = get_post_meta($post->ID, '_product_image_url', true);
    echo '<input type="text" name="_product_image_url" value="' . esc_attr($product_image_url) . '" />';
}

add_action('woocommerce_process_product_meta', 'product_image_url_save');

add_action('woocommerce_process_product_meta', 'product_image_url_save');

function product_image_url_save($post_id) {

    $product_image_url = $_POST['_product_image_url'];

    if (!empty($product_image_url)) {

        update_post_meta($post_id, '_product_image_url', sanitize_text_field($product_image_url));

        $image_url = esc_url($product_image_url);

        // Download the image and save it to the media library
        $upload = media_sideload_image($image_url, $post_id, '', 'id');

        if (!is_wp_error($upload)) {
            $image_id = $upload;
            set_post_thumbnail($post_id, $image_id);
        }
    }

}

或者您可以使用woocommerce_product_get_image挂钩的其他替代方法。

/*
Plugin Name: Set Product Images from URL
Plugin URI:
Description: Set product images from an external URL to promote affiliate network
Version: 1.0
Author:
Author URI:
License: GPL2
*/

add_action('add_meta_boxes', 'product_image_url_meta_box');

function product_image_url_meta_box() {
    add_meta_box(
        'product_image_url',
        __('Product Image URL'),
        'product_image_url_meta_box_callback',
        'product',
        'side',
        'high'
    );
}

function product_image_url_meta_box_callback($post) {
    $product_image_url = get_post_meta($post->ID, '_product_image_url', true);
    echo '<input type="text" name="_product_image_url" value="' . esc_attr($product_image_url) . '" />';
}

add_action('woocommerce_process_product_meta', 'product_image_url_save');

function product_image_url_save($post_id) {
    $product_image_url = $_POST['_product_image_url'];

    if (!empty($product_image_url)) {
        update_post_meta($post_id, '_product_image_url', sanitize_text_field($product_image_url));
    }
}

// Set the product image HTML markup based on the URL
add_filter('woocommerce_product_get_image', 'product_image_url_html_markup', 10, 2);

function product_image_url_html_markup($html, $product) {
    $product_image_url = get_post_meta($product->get_id(), '_product_image_url', true);
    if (!empty($product_image_url)) {
        $html = '<img src="' . esc_url($product_image_url) . '" alt="' . esc_attr($product->get_name()) . '" class="attachment-shop_catalog size-shop_catalog wp-post-image" />';
    }
    return $html;
}
英文:

You can use media_sideload_image() function is used to download the image from the URL and save it to the media library. This updated code will allow you to set the product image by entering an external URL.

/*
Plugin Name: Set Product Images from URL 
Plugin URI: 
Description: Set product images from an external URL to promote affiliate network 
Version: 1.0
Author: 
Author URI: 
License: GPL2
*/

add_action( &#39;add_meta_boxes&#39;, &#39;product_image_url_meta_box&#39; );

function product_image_url_meta_box() {
    add_meta_box( 
        &#39;product_image_url&#39;,
        __( &#39;Product Image URL&#39; ),
        &#39;product_image_url_meta_box_callback&#39;, 
        &#39;product&#39;,
        &#39;side&#39;,
        &#39;high&#39;
    );
}

function product_image_url_meta_box_callback( $post ) {
    $product_image_url = get_post_meta( $post-&gt;ID, &#39;_product_image_url&#39;, true );
    echo &#39;&lt;input type=&quot;text&quot; name=&quot;_product_image_url&quot; value=&quot;&#39; . esc_attr( $product_image_url ) . &#39;&quot; /&gt;&#39;;
}

add_action( &#39;woocommerce_process_product_meta&#39;, &#39;product_image_url_save&#39; );

add_action( &#39;woocommerce_process_product_meta&#39;, &#39;product_image_url_save&#39; );

function product_image_url_save( $post_id ) { 

	$product_image_url = $_POST[&#39;_product_image_url&#39;];

	if ( ! empty( $product_image_url ) ) {
    	
    	update_post_meta( $post_id, &#39;_product_image_url&#39;, sanitize_text_field( $product_image_url ) );

        $image_url = esc_url( $product_image_url );
        
        // Download the image and save it to the media library
        $upload = media_sideload_image( $image_url, $post_id, &#39;&#39;, &#39;id&#39; );
        
        if ( ! is_wp_error( $upload ) ) {
            $image_id = $upload;
	    	set_post_thumbnail($post_id, $image_id);
        }
	}

}

Or other alternative you can use that you can use woocommerce_product_get_image hook.

	/*
Plugin Name: Set Product Images from URL 
Plugin URI: 
Description: Set product images from an external URL to promote affiliate network 
Version: 1.0
Author: 
Author URI: 
License: GPL2
*/

add_action( &#39;add_meta_boxes&#39;, &#39;product_image_url_meta_box&#39; );

function product_image_url_meta_box() {
    add_meta_box( 
        &#39;product_image_url&#39;,
        __( &#39;Product Image URL&#39; ),
        &#39;product_image_url_meta_box_callback&#39;, 
        &#39;product&#39;,
        &#39;side&#39;,
        &#39;high&#39;
    );
}

function product_image_url_meta_box_callback( $post ) {
    $product_image_url = get_post_meta( $post-&gt;ID, &#39;_product_image_url&#39;, true );
    echo &#39;&lt;input type=&quot;text&quot; name=&quot;_product_image_url&quot; value=&quot;&#39; . esc_attr( $product_image_url ) . &#39;&quot; /&gt;&#39;;
}

add_action( &#39;woocommerce_process_product_meta&#39;, &#39;product_image_url_save&#39; );

function product_image_url_save( $post_id ) { 
    $product_image_url = $_POST[&#39;_product_image_url&#39;];
    
    if ( ! empty( $product_image_url ) ) {
        update_post_meta( $post_id, &#39;_product_image_url&#39;, sanitize_text_field( $product_image_url ) );
    }
}

// Set the product image HTML markup based on the URL
add_filter( &#39;woocommerce_product_get_image&#39;, &#39;product_image_url_html_markup&#39;, 10, 2 );
function product_image_url_html_markup( $html, $product ) {
    $product_image_url = get_post_meta( $product-&gt;get_id(), &#39;_product_image_url&#39;, true );
    if ( ! empty( $product_image_url ) ) {
        $html = &#39;&lt;img src=&quot;&#39; . esc_url( $product_image_url ) . &#39;&quot; alt=&quot;&#39; . esc_attr( $product-&gt;get_name() ) . &#39;&quot; class=&quot;attachment-shop_catalog size-shop_catalog wp-post-image&quot; /&gt;&#39;;
    }
    return $html;
}

huangapple
  • 本文由 发表于 2023年7月6日 20:45:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定