Ajax XML-RPC Library for WordPress

WordPress XML-RPC Library that communicates with XMLHttpRequest requests. Both the arguments passed and the return variable are processed as JavaScript objects.

The following WordPress XMLRPC methods are available within the lib

  • wp.getUsersBlogs
  • wp.getCommentCount
  • wp.deleteComment
  • wp.editComment
  • wp.newComment
  • wp.getCommentStatusList
  • wp.getComment
  • wp.getComments
  • wp.getPage
  • wp.getOptions
  • metaWeblog.newPost

GitHub project page

How to use it:

var connection = new AddBlogConn(“username” , “password”, “xmlrpc endpoint URL”);
connection.addListener(new AddBlogsListener());
connection.startConn();

The Listener should implement at leat these three methods:

function AddBlogsListener(){
}
AddBlogsListener.prototype.connRequestError = function(errorMsg){
EW.LogSystem.error(“AddBlogsListener.connRequestError”);
}
AddBlogsListener.prototype.connRequestStopped = function(){
EW.LogSystem.debug(“AddBlogsListener.connRequestStopped”);
}

AddBlogsListener.prototype.connRequestCompleted = function(userBlogs){
EW.LogSystem.debug(“AddBlogsListener.connRequestCompleted”);
}

GitHub project page

23 thoughts on “Ajax XML-RPC Library for WordPress

  1. Pingback: Testing a Wordpress Ajax XML-RPC Library | sometimes infinity is a loop

  2. Bill Fisher

    Thank you for your work on this. I have it working in a basic form, and I will be continuing to work on it. Any plans to move this to GitHub or BitBucket so people can contribute?

    Reply
    1. Danilo Post author

      Hi, this is an example of NewPost:


      /*
      * The struct content can contain the following standard keys:
      * title, for the title of the entry;
      * description, for the body of the entry;
      * dateCreated, to set the created-on date of the entry;
      */
      var content = {
      title : $("newPostTitle").value,
      description : $("newPostContent").value,
      mt_keywords : $("newPostTags").value
      }

      try {
      this.connection = new NewPost(this.blogObj.username, this.blogObj.password, this.blogObj.xmlrpc, content);
      this.connection.addListener(this);
      this.connection.startConn();
      }
      catch(errrrrrr) {
      WP.Controller.showErrorDialog ("Err", errrrrrr);
      }

      Reply
      1. Stephen

        Thank you so much!! That worked great 🙂

        I wish I was better at using GitHub, as i have modified the code so that if “window” doesn’t exist, it still works…. as I am inside Appcelerator Titanium (JS-based dev environment for Mobile apps) and there is no DOM….

        Reply
      2. Stephen

        btw, one more hopefully super simple question

        where can I find a list of all the parameters available to set on that “content” object? Your example shows how to add keywords, but i wish to put a post in a specific category (or categories if possible)

        like your example has “mt_keywords”, but that isn’t specified here on WP’s codex page about XML-RPC: http://codex.wordpress.org/XML-RPC_wp#Parameters_16

        Reply
          1. Stephen

            bam, an array for the property “categories” did the trick!

            once I get some time, I’m going to get on board with learning github and maybe I’ll post the updates in case “window” doesn’t exist… this library has super useful possibilities in the mobile dev arena 🙂

            Thanks Danilo

  3. Bill Fisher

    I keep getting “Origin null is not allowed by Access-Control-Allow-Origin” in Chrome and some nasty warnings in Firefox. Am I doing something wrong? Does this library provide a way around the same origin policy?

    My code looks like this:

    var testPost = function(){
    var title = document.forms[0].postTitle.value,
    description = document.forms[0].postDescription.value,
    username = document.forms[0].username.value,
    password = document.forms[0].password.value,
    blog = document.forms[0].blog.value;
    var postContent = {
    “title”: title,
    “description”: description
    }
    var post = new NewPost(username , password, blog + “/xmlrpc.php”, postContent);
    post.execute(); //also tried post.startConn();
    };

    Reply
    1. Grsmto

      I got exactly the same problem. It doesn’t work at all on Chrome/Safari and getting an security alert on Firefox. I wanted to deal with it for my Android webapp but It trigger the “error” listener on both browsers…

      Reply
      1. Danilo Post author

        @Grsmto — it should work fine within a web-app (widget) since cross domain calls are not applied. I used this library in a Nokia widget without issues. On desktop browser you should disable the cross domain calls restrictions. For example on Chrome for mac you can use this string

        /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome –allow-file-access-from-files –disable-web-security –enable-file-cookies –disk-cache-size=1 –media-cache-size=1

        On firefox you should set the ‘signed.applets.codebase_principal_support=true’ at about:config during the developing phase.

        Reply
    1. Danilo Post author

      Hey fatma033, I’ve also received a direct email from you with questions on comments management. Will reply to your questions here just to keep a public record of it.
      I suggest you to take a look at the following project – https://github.com/daniloercoli/MoPress-for-Nokia – It uses an old version of the JS-XML-RPC library but it shows you how to use comments call.
      If you want to assign categories to a new post, you need to pass the array of ‘terms’ in he new post XML-RPC call according to the WordPress XML-RPC documentation here: http://codex.wordpress.org/XML-RPC_WordPress_API

      If you want to use the old APIs take a look at the documentation here: http://codex.wordpress.org/XML-RPC_Support – For eg if you want to use metaWeblog.newPost you need to pass the categories as array in the request document.

      Reply
      1. fatma033

        Thank you for your response.
        My application is into key USB. I used Google Chrome. I succeeded to add a new post with “title”, “description”, “mt_keywords” but not with “categories” (category exists in my blog) and “date_created_gmt”. Here is my code:
        function ISODateString(d){
        function pad(n){return n<10 ? '0'+n : n}
        return d.getUTCFullYear()+'-'
        + pad(d.getUTCMonth()+1)+'-'
        + pad(d.getUTCDate())+'T'
        + pad(d.getUTCHours())+':'
        + pad(d.getUTCMinutes())+':'
        + pad(d.getUTCSeconds())+'Z'}

        var date = new Date();
        var d=(ISODateString(date));
        titre=$("#titrem").val();
        desc=$("#contenum").val();
        //content
        var content = {
        title: titre,
        description: desc,
        date_created_gmt: d,
        categories: "police",
        mt_keywords : "un",
        }
        //envoyer un post
        try {
        this.connection = new NewPost("fatma033" , "xxxxxxx", "http://untitre.wordpress.com/xmlrpc.php&quot;, content);
        this.connection.addListener(this);
        this.connection.startConn();}

        Reply
          1. fatma033

            thank you. It works:

            var tab= new Array(“police”);
            var content{
            title : titre,
            description : desc,
            categories: tab,
            }
            In the code above i call the function ISODateString() but date_created_gmt does not work.
            Please can you help me.

  4. japonte

    Hi, I am trying to use this library from phonegap, is there any way of sending an article with a photo of the phone?

    Reply
    1. Danilo Post author

      You need to get the bits of the Image and Base64 Encode them before using the NewMedia connection. There are several ways of getting the bits. Mostly depend on the framework your are using. I suggest you to use PhoneGap functions to get the content of the file from memory.

      In situations where you can’t access the File System of the device you can use the Canvas HTML object like I did here:
      https://github.com/daniloercoli/WordPress-BlackBerry10-alpha/blob/master/js/screens/test_filters.js#L263

      • Put the picture in an img tag
      • Draw the picture in the Canvas
      • You can even manipulates the bits of the image. (In the example above I applied a BlackAndWhite filter)
      • Get the bits of the pictures from the canvas var imageData = canvas.toDataURL();
      • Base64 Encode them new Base64(imageData, true)

      Note that this way of getting the bits of picture will use a lot of memory on the device. It’s not always applicable due to memory limitation you may encounter in some devices.

      Reply
        1. Danilo Post author

          You need to use custom fields when editing/creating a post. Check the code of the iOS app, or the BlackBerry app.
          Basically you need read the location by using PhoneGap API, and then put those data in the correct Custom Fields of the post.
          Once uploaded to the server you may need a plugin, like the following http://wordpress.org/extend/plugins/geolocation/, to display location data on the page.

          Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s