• 注册
  • 交流大厅 交流大厅 关注:28 内容:32

    Woocomerce 采集产品图片远程直接调用的改造

  • 查看作者
  • 打赏作者
  • 1 /wp-content/plugins/woocommerce/includes/wp-rest-functions.php

    修改wc_rest_upload_image_from_url 函数 为以下代码

     function wc_rest_upload_image_from_url( $image_url ) {
        
        $file['file']="$image_url";
         $file['url']=$image_url;
         $file['type']="image/jpeg";
         do_action( 'woocommerce_rest_api_uploaded_image_from_url', $file, $image_url );
         return $file;
    	 }
    	 	

    2 wp-includes/post.php

    修改 wp_get_attachment_url函数为以下代码

    function wp_get_attachment_url( $attachment_id = 0 ) {
    	global $pagenow;
    
    	$attachment_id = (int) $attachment_id;
    
    	$post = get_post( $attachment_id );
    
    	if ( ! $post ) {
    		return false;
    	}
    
    	if ( 'attachment' !== $post->post_type ) {
    		return false;
    	}
    
    	$url = '';
    	// Get attached file.
    	$file = get_post_meta( $post->ID, '_wp_attached_file', true );  
    	if ( $file ) {
    		// Get upload directory.
    		$uploads = wp_get_upload_dir();
    		
    	 	 if(str_starts_with($file, "http://")||str_starts_with($file, "https://"))
    	 	 {
    	 	     $url=$file;
    	 	 }
    		 else if ( $uploads && false === $uploads['error'] ) {
    			// Check that the upload base exists in the file location.
    			if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
    				// Replace file location with url location.
    				$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
    			} elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) {
    				// Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
    				$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file );
    			} else {
    				// It's a newly-uploaded file, therefore $file is relative to the basedir.
    				$url = $uploads['baseurl'] . "/$file";
    			}
    		}
    	}
    
    	/*
    	 * If any of the above options failed, Fallback on the GUID as used pre-2.7,
    	 * not recommended to rely upon this.
    	 */
    	 
    	if ( ! $url ) {
    		$url = get_the_guid( $post->ID );
    	}
    
    	// On SSL front end, URLs should be HTTPS.
    	if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
    		$url = set_url_scheme( $url );
    	}
    
    	/**
    	 * Filters the attachment URL.
    	 *
    	 * @since 2.1.0
    	 *
    	 * @param string $url           URL for the given attachment.
    	 * @param int    $attachment_id Attachment post ID.
    	 */
    
    	$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
    
    	if ( ! $url ) {
    		return false;
    	}
    
    	return $url;
    }

  • 请登录之后再进行评论

    登录

    WordPress后台-外观-小工具 进行配置小工具

    帖子间隔 侧栏位置: