maxcutler / python-wordpress-xmlrpc

Python library for WordPress XML-RPC integration

Home Page:http://python-wordpress-xmlrpc.rtfd.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to post HTML tag on content?

xubiuit opened this issue · comments

Hi,

Please let me know how to insert the html tag inside the post.content.
For example:
post.content = '< div class="abc">Hellow Word</ div>'

Regards,

@xubiuit , you will need to also set MIME Type for your post using the below line.

post.mime_type = "text\html";

I had also faced this issue before. Just ran dir(post) and saw this mime_type
property defined on post.

It worked. Nice.

Have a look at my sample code below, it will help you a lot.
You can also get this example at https://github.com/maxcutler/python-wordpress-xmlrpc/wiki/Creating-blog-post. I thought to add it to Wiki to help others.

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.users import GetUserInfo
from wordpress_xmlrpc.methods import posts

# url, username, password
client = Client('http://rishikesh67.pythonanywhere.com/xmlrpc.php', \
  'hygull', '^password@123$')

post = WordPressPost()

post.title = 'HTML5 - Intoduction'

# HTML content
content = """<!DOCTYPE html>
<html>
<head>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
</head>
<body>

<h2>HTML Table</h2>

<table>
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
  <tr>
    <td>Ernst Handel</td>
    <td>Roland Mendel</td>
    <td>Austria</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>Helen Bennett</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Laughing Bacchus Winecellars</td>
    <td>Yoshi Tannamuri</td>
    <td>Canada</td>
  </tr>
  <tr>
    <td>Magazzini Alimentari Riuniti</td>
    <td>Giovanni Rovelli</td>
    <td>Italy</td>
  </tr>
</table>

</body>
</html>
"""

post.content = content
post.mime_type = "text/html"

post.terms_names = {
  'post_tag': ['html5', 'table'],
  'category': ['Introductions', 'HTML5']
}
post.post_status = "publish"
post_id = client.call(posts.NewPost(post))

print "Post with id ", post_id, "successfully published"

# Getting list of posts
published_posts = client.call(posts.GetPosts({'post_status': 'publish'}))
print published_posts