I try to upload image in WordPress using XML-RPC python. But I cannot set image alt attribute for image while uploading it to wordpress. How do I add it?
This is my code to upload image in wordpress in python 3.7
:
# for wordpress
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import NewPost, GetPosts, GetPost, EditPost
from wordpress_xmlrpc.methods import media
from wordpress_xmlrpc.compat import xmlrpc_client
class WP_CLIENT():
def __init__(self, url, username, passwd):
self.url = url
self.username = username
self.passwd = passwd
self.client = Client(url, username, passwd)
# upload image
def upload_image(self, path, name):
# get the file type
file_type = 'image/'+path.split('.')[-1].lower()
# change jpg to jpeg
file_type = file_type.replace('jpg', 'jpeg')
# prepare metadata
data = {
'name': name,
'type': file_type,
}
# read the binary file and let the XMLRPC library encode it into base64
with open(path, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = self.client.call(media.UploadFile(data))
# for referance purpose
# response == {
# 'id': 6,
# 'file': 'picture.jpg'
# 'url': 'http://www.example.com/wp-content/uploads/2012/04/16/picture.jpg',
# 'type': 'image/jpeg',
# }
return response
How to add image alt attribute while uploading images in WordPress?
Thanks in advance
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.