Im trying to write a Python script that will upload images and pdf to WordPress. I would like the images to be uploaded to the folder '/wp-content/uploads/' and the pdf files to the folder '/wp-content/uploads/woocommerce_uploads/'.
我正在尝试编写一个Python脚本,可以将图片和pdf上传到WordPress。我希望图像上传到文件夹‘/wp-Content/Uploads/’,将pdf文件上传到文件夹‘/wp-Content/Uploads/WooCommerce_Uploads/’。
import requests
import os
class UploadProductMedia:
def __init__(self, media_endpoint, wordpress_username, wordpress_password):
self.media_endpoint = media_endpoint
self.wordpress_username = wordpress_username
self.wordpress_password = wordpress_password
pass
def upload_media(self, file_path):
print(f"Uploading media {file_path}")
with open(file_path, 'rb') as file:
# Prepare the data to send in the request
data = {
'file': (os.path.basename(file_path), file),
}
# Determine the upload directory based on the file type
upload_directory = '/wp-content/uploads/'
if file_path.lower().endswith(('.pdf')):
upload_directory = '/wp-content/uploads/woocommerce_uploads/'
print(f"Uploading to directory {upload_directory}")
# Send a POST request to the media endpoint with authentication
response = requests.post(f"{self.media_endpoint}?upload_to={upload_directory}", auth=(self.wordpress_username, self.wordpress_password), files=data)
if response.status_code == 201:
response_data = response.json()
uploaded_media_uri = response_data.get('guid', {}).get('rendered', '')
print(f'Successfully uploaded: {file_path}')
return uploaded_media_uri
else:
print(f'Failed to upload: {file_path}')
print(f'Response: {response.status_code} - {response.text}')
return None
if __name__ == "__main__":
# Example usage:
print("=== Starting ===")
for file in ["TestData\pear1.png", "TestData\pear1.pdf"]:
UploadProductMedia('https://mysite.co.uk/wp-json/wp/v2/media', 'myusername', 'mypassword').upload_media(file)
print("=== Finished ===")
The console logs indicate that the program worked as expected.
控制台日志显示程序运行正常。
=== Starting ===
Uploading media TestData\pear1.png
Uploading to directory /wp-content/uploads/
Successfully uploaded: TestData\pear1.png
Uploading media TestData\pear1.pdf
Uploading to directory /wp-content/uploads/woocommerce_uploads/
Successfully uploaded: TestData\pear1.pdf
=== Finished ===
However, the pdf file was uploaded to the incorrect directory. The pdf was uploaded to
https://mysite.co.uk/wp-content/uploads/2023/09/pear1.pdf
但是,pdf文件被上传到了错误的目录。该pdf已上载到https://mysite.co.uk/wp-content/uploads/2023/09/pear1.pdf
Please, can you help explain why the pdf files are not being uploaded to /wp-content/uploads/woocommerce_uploads/
请您解释一下为什么没有将pdf文件上传到/wp-content/ploads/wooCommerce_ploadds/
更多回答
优秀答案推荐
In case anyone encounters the same issue. I have been unable to find a solution to the media upload issue. I implemented a workaround by uploading files to the web server using SFTP. The SFTP code is in the repository https://github.com/seafooood/WooSpeedy/blob/main/ServiceUploadProductFtp.py
以防任何人遇到同样的问题。我一直无法找到媒体上传问题的解决方案。我使用SFTP将文件上传到Web服务器,从而实现了一种解决方法。Sftp代码位于存储库https://github.com/seafooood/WooSpeedy/blob/main/ServiceUploadProductFtp.py中
更多回答
我是一名优秀的程序员,十分优秀!