英文:
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:
<?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( '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 ) );
}
}
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;
}
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( '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);
}
}
}
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( '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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论